Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #84195
| Subject | Re: Function pointer size (was Re: nullptr is not 0) |
|---|---|
| Newsgroups | comp.lang.c++ |
| References | (5 earlier) <t61tn1$1gd6$1@gioia.aioe.org> <t62ddu$41b$1@dont-email.me> <t65lls$1nug$1@gioia.aioe.org> <t65n6s$ak9$1@dont-email.me> <t65ttj$kf5$1@dont-email.me> |
| From | Richard Damon <Richard@Damon-Family.org> |
| Message-ID | <UmvhK.803$vAW9.197@fx10.iad> (permalink) |
| Organization | Forte - www.forteinc.com |
| Date | 2022-05-19 13:45 -0400 |
On 5/19/22 1:14 PM, Andrey Tarasevich wrote:
> On 5/19/2022 8:19 AM, Alf P. Steinbach wrote:
>> On 19 May 2022 16:53, Juha Nieminen wrote:
>>> Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
>>>>> If you have a reference or pointer to an object of type class A, and a
>>>>> member pointer to a member function of class A, which is virtual, if
>>>>> the actual object pointed to is of some derived type B which has its
>>>>> own specialization of that virtual function, calling the function
>>>>> using
>>>>> the pointer will call the derived implementation not, the one in
>>>>> class A.
>>>>>
>>>>> For this to be possible the pointer to member function needs
>>>>> additional
>>>>> data.
>>>>
>>>> No, that's incorrect.
>>>>
>>>> This has nothing to do with virtual functions. On the contrary,
>>>> pointers-to-virtual-functions are "easy": regular virtual call
>>>> mechanism
>>>> itself is already required to incorporate all necessary mechanics to
>>>> properly invoke virtual functions across the entire hierarchy of
>>>> classes. This mechanics is already present in "regular" virtual calls
>>>> (without involving any pointers-to-members).
>>>
>>> I'm not sure that's correct.
>>>
>>> When you have a pointer-to-member, in the location of the call the
>>> compiler
>>> doesn't know *which* member function it's pointing to. It only knows its
>>> signature, not its name. The class may have several differently-named
>>> member fuctions with the same signature. I don't think the compiler can
>>> even know (from the source code alone) if the pointed-to function is
>>> virtual or not.
>>>
>>> Thus, the compiler needs to create code that somehow figures out if
>>> that member function is virtual, and which function it is, and then
>>> use the vtable as normal to jump to the actual most-derived
>>> implementation.
>>> I don't think it can do this without the extra data in the pointer.
>>> (Well, I suppose it theoretically could, but that might require some
>>> searching).
>>>
>>> Please correct me if I'm wrong. (Honestly. This isn't sarcasm.)
>>
>> Just an example of what you're saying:
>>
>>
>> #include <stdio.h>
>>
>> struct A
>> {
>> void foo() const { printf( "A::foo\n" ); }
>> virtual void bar() const { printf( "A::bar\n" ); }
>> };
>>
>> struct B: A
>> {
>> void bar() const override { printf( "B::bar\n" ); }
>> };
>>
>> auto main() -> int
>> {
>> auto f = &A::foo;
>> const A& o = B();
>>
>> f = &A::bar;
>> (o.*f)(); // "B::bar"
>> }
>>
>
> Not sure what this is supposed to illustrate. That
> pointers-to-member-functions implement "late binding" at the point of
> the call? Yes, that's true and that's banal.
>
> However, this still does not in any way mean that
> pointers-to-member-function has to distinguish between pointing to a
> "regular" function and pointing to a virtual function.
>
Look at an actual layout of a class
Since struct A has a virtual function in it, the class needs a vtable,
so the layout of A will be something like:
struct A:
VTable* vtbl = vtable_A; // this gets filled in by the constructor.
...rest of the data in A
vtable_a:
void (*vtbl_A_bar)(A*); // filled in by loader to point to the
implementation of A::bar
struct B:
VTable* vtbl = vtable_B; // this gets filled in by the constructor
...rest of data in A
...rest of data in B
vtable_b:
void (*vtbl_B_bar)(A*); // filled in by loader to point to the
implementation of B::bar
&A::foo results in a pointer that says it is a "normal" pointer, and the
address of A::foo;
&A::bar resutls in a pointer that says it is a "Virtual" pointer to the
first element of the vtable (which will point to A::bar in an A object
and B::bar in a B object)
There needs to be a bit when we evalute o.*f so it knows if f is storing
the actual address of the function or the offset into the vtable.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
nullptr is not 0 Sam <sam@email-scan.com> - 2022-05-08 09:45 -0400
Re: nullptr is not 0 "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-08 16:21 +0200
Re: nullptr is not 0 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-08 10:00 -0700
Re: nullptr is not 0 Bonita Montero <Bonita.Montero@gmail.com> - 2022-05-09 06:38 +0200
Re: nullptr is not 0 Juha Nieminen <nospam@thanks.invalid> - 2022-05-09 05:06 +0000
Re: nullptr is not 0 Vir Campestris <vir.campestris@invalid.invalid> - 2022-05-09 21:46 +0100
Re: nullptr is not 0 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-09 13:57 -0700
Re: nullptr is not 0 Juha Nieminen <nospam@thanks.invalid> - 2022-05-10 05:04 +0000
Re: nullptr is not 0 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-09 22:33 -0700
Re: nullptr is not 0 scott@slp53.sl.home (Scott Lurndal) - 2022-05-09 21:56 +0000
Function pointer size (was Re: nullptr is not 0) Vir Campestris <vir.campestris@invalid.invalid> - 2022-05-17 21:30 +0100
Re: Function pointer size (was Re: nullptr is not 0) scott@slp53.sl.home (Scott Lurndal) - 2022-05-17 21:00 +0000
Re: Function pointer size (was Re: nullptr is not 0) Vir Campestris <vir.campestris@invalid.invalid> - 2022-05-19 21:52 +0100
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-19 14:08 -0700
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-17 17:05 -0700
Re: Function pointer size (was Re: nullptr is not 0) Juha Nieminen <nospam@thanks.invalid> - 2022-05-18 04:46 +0000
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-18 02:14 -0700
Re: Function pointer size (was Re: nullptr is not 0) Juha Nieminen <nospam@thanks.invalid> - 2022-05-19 14:53 +0000
Re: Function pointer size (was Re: nullptr is not 0) "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-19 17:19 +0200
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-19 10:14 -0700
Re: Function pointer size (was Re: nullptr is not 0) Richard Damon <Richard@Damon-Family.org> - 2022-05-19 13:45 -0400
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-19 11:31 -0700
Re: Function pointer size (was Re: nullptr is not 0) Juha Nieminen <nospam@thanks.invalid> - 2022-05-20 06:58 +0000
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-20 00:11 -0700
Re: Function pointer size (was Re: nullptr is not 0) Richard Damon <Richard@Damon-Family.org> - 2022-05-19 12:27 -0400
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-19 09:47 -0700
Re: Function pointer size (was Re: nullptr is not 0) Richard Damon <Richard@Damon-Family.org> - 2022-05-19 13:36 -0400
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-19 11:24 -0700
Re: Function pointer size (was Re: nullptr is not 0) Richard Damon <Richard@Damon-Family.org> - 2022-05-19 21:32 -0400
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-19 20:28 -0700
Re: Function pointer size (was Re: nullptr is not 0) Richard Damon <Richard@Damon-Family.org> - 2022-05-20 10:57 -0400
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-19 09:35 -0700
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-19 10:10 -0700
Re: Function pointer size (was Re: nullptr is not 0) Juha Nieminen <nospam@thanks.invalid> - 2022-05-20 07:15 +0000
Re: Function pointer size (was Re: nullptr is not 0) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-05-18 11:50 -0700
Re: Function pointer size (was Re: nullptr is not 0) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-18 15:46 -0700
Re: Function pointer size (was Re: nullptr is not 0) "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-05-19 20:52 -0700
csiph-web