class C1 {
void A();
void B();
}
void C1::A(){ return B(); }
class C2 : public C1 {
void B();
}
C2 *obj = new C2;
obj->A(); // returns B() from C1
Why does B() from C1 called? How to make A() exist only in C1 and call B() from C2?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to make
B()inC1avirtualfunction.Virtual functions are basically function pointers that take their value upon initialization of the object. If you
new C1, the function pointer would point toC1::Bwhile if younew C2that function pointer would point toC2::B.Note: To read more about
virtualand related subjects, search for function overriding and polymorphism.