Click here to Skip to main content
15,886,963 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: virtual inheritance and polymorphism! Pin
Dean Seo3-Aug-11 16:20
Dean Seo3-Aug-11 16:20 
AnswerRe: virtual inheritance and polymorphism! Pin
QuickDeveloper1-Aug-11 21:35
QuickDeveloper1-Aug-11 21:35 
GeneralRe: virtual inheritance and polymorphism! Pin
Emilio Garavaglia2-Aug-11 20:21
Emilio Garavaglia2-Aug-11 20:21 
GeneralRe: virtual inheritance and polymorphism! Pin
Dean Seo3-Aug-11 16:23
Dean Seo3-Aug-11 16:23 
GeneralRe: virtual inheritance and polymorphism! Pin
Dean Seo3-Aug-11 16:23
Dean Seo3-Aug-11 16:23 
AnswerRe: virtual inheritance and polymorphism! Pin
Chandrasekharan P1-Aug-11 22:03
Chandrasekharan P1-Aug-11 22:03 
GeneralRe: virtual inheritance and polymorphism! Pin
Dean Seo3-Aug-11 16:23
Dean Seo3-Aug-11 16:23 
AnswerRe: virtual inheritance and polymorphism! Pin
Resmi Anna1-Aug-11 23:04
Resmi Anna1-Aug-11 23:04 
Peter has already given a partial answer to your question.
just consider the below example
class A
{
public:
	A()
	{
		cout << "A::A\n";
	}
};
class B : public A
{
public:
	B()
	{
		cout << "B::B\n";
	}
};
class C : public B
{
public:
	C()
	{
		cout << "C::C\n";
	}
};
int main()
{
	A* a = new C();
	return 0;
}

What will be the output
A::A
B::B
C::C
right??
means the order of object creation is like A->B->C
and C objects reference is being put into A's pointer
In your code
There are two object creation order

like A->B->D
and
like A->C->D
Here compler would be confused which D's reference comes to A's pointer???
That is why there is compilation error. c++ provides us something through which we can overcome this problem. Thats the virtual class.
You might have heard of diamond problem in c++. This what happens here.
you can change you code like below.
class A{};
class B : virtual public A{};
class C : virtual public A{};
class D : public B, public C{};
int _tmain(int argc, _TCHAR* argv[])
{	
	A* a = new D();   // No error!
} 

note the key word virtual.
Now the order of object creation will be like A->B->C->D. you can print some thing in constructure of each class and can see this.
In the second code snippet
class A{};
class B : public A{};
class C : public A{};
class D : public B, public C{};
int _tmain(int argc, _TCHAR* argv[])
{	
	B* b = new D();  // works! ..what?
}

There is no confusion for the compiler even though there are 2 object creation order.
One like A->B->D and another like A->C->D. But you have clearly mentioned that D objects refernce goes to B's pointer.
So complier will take A->B->D order and put this into B's pointer. But C object also will be created.

Now it is clear that why the third code snippet cause an error.
B's pointer stores a reference of D object being created in the order A->B->D. There is no Func() function on the way. Is there??
GeneralRe: virtual inheritance and polymorphism! Pin
Emilio Garavaglia2-Aug-11 20:30
Emilio Garavaglia2-Aug-11 20:30 
GeneralRe: virtual inheritance and polymorphism! Pin
Dean Seo3-Aug-11 16:24
Dean Seo3-Aug-11 16:24 
QuestionHow to disable a checkbox created using MFCRibbonCheckBox? Pin
MeghaJoshi1-Aug-11 18:22
MeghaJoshi1-Aug-11 18:22 
AnswerRe: How to disable a checkbox created using MFCRibbonCheckBox? Pin
Dr.Walt Fair, PE1-Aug-11 18:51
professionalDr.Walt Fair, PE1-Aug-11 18:51 
AnswerRe: How to disable a checkbox created using MFCRibbonCheckBox? Pin
Code-o-mat2-Aug-11 3:07
Code-o-mat2-Aug-11 3:07 
QuestionValue of an object in visual studio Pin
AndrewG12311-Aug-11 13:34
AndrewG12311-Aug-11 13:34 
AnswerRe: Value of an object in visual studio Pin
Richard Andrew x641-Aug-11 13:48
professionalRichard Andrew x641-Aug-11 13:48 
QuestionCreateNamedPipe() Pin
Member 38520241-Aug-11 12:36
Member 38520241-Aug-11 12:36 
AnswerRe: CreateNamedPipe() Pin
Richard Andrew x641-Aug-11 12:45
professionalRichard Andrew x641-Aug-11 12:45 
GeneralRe: CreateNamedPipe() Pin
Member 38520241-Aug-11 12:54
Member 38520241-Aug-11 12:54 
GeneralRe: CreateNamedPipe() Pin
Richard Andrew x641-Aug-11 12:59
professionalRichard Andrew x641-Aug-11 12:59 
GeneralRe: CreateNamedPipe() Pin
Member 38520241-Aug-11 12:59
Member 38520241-Aug-11 12:59 
GeneralRe: CreateNamedPipe() Pin
Richard Andrew x641-Aug-11 13:05
professionalRichard Andrew x641-Aug-11 13:05 
GeneralRe: CreateNamedPipe() Pin
Member 38520241-Aug-11 13:16
Member 38520241-Aug-11 13:16 
GeneralRe: CreateNamedPipe() Pin
Richard Andrew x641-Aug-11 13:23
professionalRichard Andrew x641-Aug-11 13:23 
GeneralRe: CreateNamedPipe() Pin
Member 38520241-Aug-11 13:30
Member 38520241-Aug-11 13:30 
GeneralRe: CreateNamedPipe() Pin
Richard Andrew x641-Aug-11 13:32
professionalRichard Andrew x641-Aug-11 13:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.