 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
llewelly Guest
|
Posted: Tue Aug 26, 2003 6:54 pm Post subject: Re: using member function pointers |
|
|
[email]set.me (AT) free (DOT) fr[/email] (TB) writes:
| Quote: | I'm a bit confused here...
Given
class Base { ... public: virtual void foo(); ... }
class Derived : public Base { ... public: virtual void foo(); ... }
and
template <class A> void callMethod(A *a, void (A::*f)())
{ (a->*f)(); }
if you have
Base *p = new Derived;
callMethod(p, Base::foo);
what gets called - Base::foo or Derived::foo?
|
Derived::foo.
| Quote: | In other words,
does this follow static or dynamic linkage.
[snip] |
Both. callMethod is instantiated on the static type of p, which is B*,
resulting in callMethod<B>(B *a, void (B::*f)()) . Within
callMethod<B>, (a->*f)() results in a virtual function call,
because Base::foo is a virtual function, and its address was
copied into f when callMethod<B> was called.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Tue Aug 26, 2003 7:07 pm Post subject: Re: using member function pointers |
|
|
In article <247c59f2.0308251133.1dff0cff (AT) posting (DOT) google.com>, TB wrote:
| Quote: | I'm a bit confused here...
Given
class Base { ... public: virtual void foo(); ... }
class Derived : public Base { ... public: virtual void foo(); ... }
and
template <class A> void callMethod(A *a, void (A::*f)())
{ (a->*f)(); }
if you have
Base *p = new Derived;
callMethod(p, Base::foo);
|
An "&" is required before "Base::foo".
| Quote: | what gets called - Base::foo or Derived::foo? In other words,
does this follow static or dynamic linkage.
|
What you're asking about is properly called binding, not linkage. The
answer is that a non-static member function pointer to a virtual member
function is dynamically bound, so Derived::foo is called.
| Quote: | If it follows static linkage, how can you get it to follow dynamic
linkage? And/or vice-versa?
snip |
It's not possible, so far as I know.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
TB Guest
|
Posted: Wed Aug 27, 2003 9:29 am Post subject: Re: using member function pointers |
|
|
llewelly <llewelly.at (AT) xmission (DOT) dot.com> wrote
| Quote: | set.me (AT) free (DOT) fr (TB) writes:
I'm a bit confused here...
Given
class Base { ... public: virtual void foo(); ... }
class Derived : public Base { ... public: virtual void foo(); ... }
and
template <class A> void callMethod(A *a, void (A::*f)())
{ (a->*f)(); }
if you have
Base *p = new Derived;
callMethod(p, Base::foo);
what gets called - Base::foo or Derived::foo?
Derived::foo.
In other words,
does this follow static or dynamic linkage.
[snip]
Both. callMethod is instantiated on the static type of p, which is B*,
resulting in callMethod<B>(B *a, void (B::*f)()) . Within
callMethod<B>, (a->*f)() results in a virtual function call,
because Base::foo is a virtual function, and its address was
copied into f when callMethod<B> was called.
|
So if I want callMethod to invoke Base's version (if I'm being really
twisted), how would I do it? By writing (a->Base::*f)() instead? Or
is there no way of invoking Base::foo on p in this way?
Thanks again,
TB
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|