Sign in to follow this  
_John_Galt_

Inheritance

Recommended Posts

I know this sounds weird ... but is there any way one can CALL a derived class member function using a pointer to its base class? Let me explain my problem more in detail I have a pointer to a base class function . Can this function pointer be asked to point to a function thats is there in class derived from the above mentioned base class?/

Share this post


Link to post
Share on other sites
You can, using virtual functions.

If B is derived from A, and both have a virtual member function Foo, then the version of Foo that is called depends on the type of the object when it was created, so:

A * x = new A( );
x->Foo( ); // Calls A::Foo

A * y = new B( );
y->Foo( ); // Calls B::Foo

But notice that if C is derived from B, and has no virtual member Foo:

A * z = new C( );
z->Foo( ); // Calls B::Foo since C::Foo does not exist

It is also possible to call a function of any parent on a child object, such as:

C * w = new C( );
C->A::Foo( ); // Calls the A::Foo version of Foo

Share this post


Link to post
Share on other sites
thanks ToohrVyk
But in my situation i cannot use virtual functions at all!!!

Say has anyone used Qt ... i want to implement something like the
connect function in Qt .. So any Qt ppl there u cud understand what
i am getting at...

Now any suggestions ??

Share this post


Link to post
Share on other sites
Let me explain my problem more in detail

I have a pointer to a base class function .
Can this function pointer be asked to point to a function thats is there in class derived from the above mentioned base class?/

Share this post


Link to post
Share on other sites
Assuming you know *for sure* that you have a pointer to the derived class, you can do this:


((DerivedClassType *)thePointer)->DerivedClassFunction();


Out of curiosity, why can't you use virtual functions?

Share this post


Link to post
Share on other sites
I'm not sure I understand. It would help if would you show some source code explaining what you're trying to do.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave Hunt
Assuming you know *for sure* that you have a pointer to the derived class, you can do this:


((DerivedClassType *)thePointer)->DerivedClassFunction();


Out of curiosity, why can't you use virtual functions?


If you're not sure, you can test with dynamic_cast:


DerivedClassType *derivedPointer =
dynamic_cast<DerivedClassType *>(thePointer);

if(derivedPointer) derivedPointer->DerivedClassFunction();


You do have to enable run-time type info when compiling.

BC

Share this post


Link to post
Share on other sites
Quote:
Original post by Couvillion
Quote:
Original post by Dave Hunt
Assuming you know *for sure* that you have a pointer to the derived class, you can do this:


((DerivedClassType *)thePointer)->DerivedClassFunction();


Out of curiosity, why can't you use virtual functions?


If you're not sure, you can test with dynamic_cast:


DerivedClassType *derivedPointer =
dynamic_cast<DerivedClassType *>(thePointer);

if(derivedPointer) derivedPointer->DerivedClassFunction();


You do have to enable run-time type info when compiling.

BC

...and the base class must have at least 1 virtual function.

Share this post


Link to post
Share on other sites
Quote:
Original post by _John_Galt_
Let me explain my problem more in detail

I have a pointer to a base class function .


Is this a static function in the base class, or a member function?

If the former, then the type of that variable doesn't depend on the class in use, so just point away.

If the latter (which is how I'm understanding you), then just make the function virtual, and it should behave as you'd like. All you need to do is bind the member function pointer to a derived class instance; then when it is dereferenced, the derived class' member function will be called. But if the PMF is already bound, you're SOL AFAICT. [wink]

(Actually, A quick google search reveals that GCC offers an extension that will let you extract a 'raw' function pointer from a bound pointer-to-member-function, where the arguments accepted are an instance of the object that would be bound, followed by the usual arguments. So if given a pointer already bound to some base instance, it looks like you could do the extraction, then call with some derived instance. But I don't at all guarantee or even necessarily expect that to work, even substituting another base instance instead of the original.)

Anyway, as is usual when something sounds this complicated, you may want to rethink your design. [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this