Click here to Skip to main content
15,888,011 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: "choose color" combobox Pin
Graham Bradshaw27-Jan-06 5:36
Graham Bradshaw27-Jan-06 5:36 
AnswerRe: "choose color" combobox Pin
David Crow27-Jan-06 9:41
David Crow27-Jan-06 9:41 
Questionhttp://www.codeproject.com/listctrl/liwne.asp Pin
hanno2527-Jan-06 4:48
hanno2527-Jan-06 4:48 
AnswerRe: http://www.codeproject.com/listctrl/liwne.asp Pin
benjymous27-Jan-06 5:41
benjymous27-Jan-06 5:41 
QuestionInheritance question Pin
act_x27-Jan-06 4:39
act_x27-Jan-06 4:39 
QuestionRe: Inheritance question Pin
David Crow27-Jan-06 5:10
David Crow27-Jan-06 5:10 
AnswerRe: Inheritance question Pin
Maximilien27-Jan-06 5:12
Maximilien27-Jan-06 5:12 
AnswerRe: Inheritance question Pin
ddmcr27-Jan-06 7:41
ddmcr27-Jan-06 7:41 
act_x wrote:
whose copy of Foo() will i call ?


None. The code will not even compile.
And adding virtual keyword to B and C classes does not have a sense in this case as the existance of class A.

Try compiling next code, it will not compile also :

#include "stdafx.h"
#include <iostream>
using namespace std;

class B 
{
public :
virtual void foo() { } ;

};
class C 
{
public :
virtual void foo() { } ;
};

class D : public B , public C
{
public :
} ;

int _tmain(int argc, _TCHAR* argv[])
{
D *ptr = new D ;
ptr->foo(); // Because here compiler does not know which <code>foo()</code> to call
delete ptr;
return 0;
}


Adding virtual keyword assures that D will inherit just one copy of members of A.

Consider we have next code :

#include "stdafx.h"
#include <iostream>
using namespace std;

class A
{
public :
virtual void GetX() { cout<<X; } ;
int X;
};

class B : public virtual A
{
public :
	void setXB() {X = 20;}
};

class C : public virtual A
{
public :
	void setXC() {X = 30;}
};

class D : public B , public C
{
public :
} ; 

int _tmain(int argc, _TCHAR* argv[])
{
	D *p = new D();
	p->setXC();	
	p->setXB();
	p->GetX(); /* If classes B and C were not declared <code>virtual </code>then the 
compiler would generate here error.As it would not know which value of <code>X</code> to print 20 or 30 ?. As A would be inherited "seperately twice" by B and C. */

delete p;
return 0;

}


PS. By adding virtual keyword to B and C classes we are sure that B and C " share same instance of class A ".
Therefore output will be 20.







"Success is the ability to go from one failure to another with no loss of enthusiasm." - W.Churchill




-- modified at 14:06 Friday 27th January, 2006

QuestionPassing a 2 dimension array created with new Pin
fleetmanager27-Jan-06 4:32
fleetmanager27-Jan-06 4:32 
QuestionRe: Passing a 2 dimension array created with new Pin
David Crow27-Jan-06 9:45
David Crow27-Jan-06 9:45 
QuestionAuto Enable Menu for dialog. Pin
includeh1027-Jan-06 4:02
includeh1027-Jan-06 4:02 
AnswerRe: Auto Enable Menu for dialog. Pin
Maximilien27-Jan-06 4:56
Maximilien27-Jan-06 4:56 
QuestionYour comments for monitoring users Pin
includeh1027-Jan-06 4:00
includeh1027-Jan-06 4:00 
AnswerRe: Your comments for monitoring users Pin
David Crow27-Jan-06 4:36
David Crow27-Jan-06 4:36 
AnswerRe: Your comments for monitoring users Pin
Maximilien27-Jan-06 7:12
Maximilien27-Jan-06 7:12 
QuestionProblem in moving the hole in a puzzle game Pin
eivanlo27-Jan-06 3:53
eivanlo27-Jan-06 3:53 
AnswerRe: Problem in moving the hole in a puzzle game Pin
David Crow27-Jan-06 4:34
David Crow27-Jan-06 4:34 
QuestionCompiles fine, then doesn't... Pin
the_augy27-Jan-06 2:41
the_augy27-Jan-06 2:41 
AnswerRe: Compiles fine, then doesn't... Pin
Rage27-Jan-06 2:59
professionalRage27-Jan-06 2:59 
GeneralRe: Compiles fine, then doesn't... Pin
the_augy29-Jan-06 23:54
the_augy29-Jan-06 23:54 
QuestionRe: Compiles fine, then doesn't... Pin
David Crow30-Jan-06 2:52
David Crow30-Jan-06 2:52 
QuestionDrag and Drop Pin
Anil_vvs27-Jan-06 1:19
Anil_vvs27-Jan-06 1:19 
QuestionTooltip popup on pointing to text. As a plugin in VS .NET Pin
AlnKG27-Jan-06 0:58
AlnKG27-Jan-06 0:58 
AnswerRe: Tooltip popup on pointing to text. As a plugin in VS .NET Pin
Aravind Badrinath Krishnan27-Jan-06 23:28
Aravind Badrinath Krishnan27-Jan-06 23:28 
QuestionShow or Hide image Pin
waxie27-Jan-06 0:42
waxie27-Jan-06 0:42 

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.