Jump to content
  • Advertisement
Sign in to follow this  
_John_Galt_

Inheritance

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!