Inheritance
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?/
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
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
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 ??
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 ??
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?/
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?/
Assuming you know *for sure* that you have a pointer to the derived class, you can do this:
Out of curiosity, why can't you use virtual functions?
((DerivedClassType *)thePointer)->DerivedClassFunction();
Out of curiosity, why can't you use virtual functions?
I'm not sure I understand. It would help if would you show some source code explaining what you're trying to do.
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
Quote:Original post by CouvillionQuote: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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement