By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,807 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

Can a poninter of base class hold object of class derived from the derived class

P: 2
I have a question, in multilevel inheritance:

Class Base
{
};

Class derv1:public Base
{
}

Class derv2:public derv1
{
}

Is ptrBase = &objderv2; valid?

If yes, then if we have virtual method in Base class will VTABLE of granchild(derv2) class have function address of this virtual method?

Thanks
Apr 1 '08 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 849
Base* ptrBase = &objDerv2; is fine. If you add a virtual method to Base, derv1 and derv2 must redefine those by the rules of virtual functions, but once that happens, the Vtable will include it.
Apr 1 '08 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
If you add a virtual method to Base, derv1 and derv2 must redefine those by the rules of virtual functions
This only applies to pure virtual functions. Otherwise, the virtual function is inherited like any other function and its address ends up in the derived class VTBL.
Apr 2 '08 #3

P: 2
Thanks for the reply.
Apr 2 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.