Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
class A{
public:
	A(){msg();}
	virtual void msg(){AfxMessageBox(_T("A"));}
};

class B: public A{
public:
	B():A(){}
private:
	virtual void msg(){AfxMessageBox(_T("B"));}
};

void test(){
	B b;	//will Message("A");  -------why? I think I can get Message("B")!-----

	A*pA=&b;
	pA->msg();//will Message("B");
}
Posted
Comments
Maciej Los 22-Aug-13 5:40am    
And where is a question?
Thanks7872 22-Aug-13 5:41am    
That's the puzzle. :(
Maciej Los 22-Aug-13 5:46am    
;)
Stefan_Lang 22-Aug-13 7:39am    
It is in the comments, first line of function test().
Richard MacCutchan 22-Aug-13 5:46am    
This is easy enough to test for yourself.

EDIT: Richard MacCutchan came up with a good idea of writing a tip/trick about this problem. I've just put together a small discussion with example code about this problem: Never call virtual methods from the constructor or destructor of your classes in C++/C#/Java![^]

I am lazy to write here the answer again as I have already answered this question a few times. The reason in a few words: Never call virtual methods directly or indirectly from (base class) constructors. Please read my posts regarding this:

http://www.codeproject.com/Messages/4619258/Re-Serial-Port-OK-in-class-constructor-but-not-out.aspx[^]
http://www.codeproject.com/Messages/4619708/Re-Serial-Port-OK-in-class-constructor-but-not-out.aspx[^]
 
Share this answer
 
v2
Comments
Stefan_Lang 22-Aug-13 7:35am    
That first link is a bit ... compressed and may require checking previous parts of that thread. I'll give you a 5 for that second link however. Great posting.
pasztorpisti 22-Aug-13 7:51am    
Thank you!
CPallini 22-Aug-13 8:02am    
5.
pasztorpisti 22-Aug-13 8:06am    
Thank you!
Richard MacCutchan 22-Aug-13 8:10am    
5: The description in link 2 deserves publication as a Tip at the least.
Quote:
B b; //will Message("A"); -------why? I think I can get Message("B")!-----
B constructor explicitely calls the A constructor, hence you get such a message.

Sorry, that was wrong, as pasztorpisti pointed out. You may find correct info here:
C++ FAQ: [23.5] When my base class's constructor calls a virtual function on its this object, why doesn't my derived class's override of that virtual function get invoked?[^]



Quote:
pA->msg();//will Message("B");
This is the usual polimorphic behaviour.
 
Share this answer
 
v2
Comments
pasztorpisti 22-Aug-13 6:14am    
No, the message "A" is a bug in the code and it has nothing to do with the explicit call. The explicit call does exactly the same that the auto-generated ctor upcall would do.
CPallini 22-Aug-13 7:15am    
You are right I have updated my answer.
Stefan_Lang 22-Aug-13 7:36am    
Great link. I thought to look that one up when I saw the question, but you obviously beat me to it :-)

P.S.: upvoted on the basis of version 2
CPallini 22-Aug-13 8:02am    
Thank you.
pasztorpisti 22-Aug-13 7:53am    
+5, much better now :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900