C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: using member function pointers

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
llewelly
Guest





PostPosted: Tue Aug 26, 2003 6:54 pm    Post subject: Re: using member function pointers Reply with quote



[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





PostPosted: Tue Aug 26, 2003 7:07 pm    Post subject: Re: using member function pointers Reply with quote



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





PostPosted: Wed Aug 27, 2003 9:29 am    Post subject: Re: using member function pointers Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.