Click here to Skip to main content
15,893,814 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Cppunit in Visual studio 2010 Pin
jschell24-Sep-12 12:39
jschell24-Sep-12 12:39 
QuestionLate Binding Concept Pin
AmbiguousName23-Sep-12 20:30
AmbiguousName23-Sep-12 20:30 
AnswerRe: Late Binding Concept Pin
CPallini23-Sep-12 22:27
mveCPallini23-Sep-12 22:27 
AnswerRe: Late Binding Concept Pin
Richard MacCutchan23-Sep-12 23:05
mveRichard MacCutchan23-Sep-12 23:05 
QuestionRe: Late Binding Concept Pin
CPallini23-Sep-12 23:58
mveCPallini23-Sep-12 23:58 
AnswerRe: Late Binding Concept Pin
Richard MacCutchan24-Sep-12 0:11
mveRichard MacCutchan24-Sep-12 0:11 
AnswerRe: Late Binding Concept Pin
Richard MacCutchan23-Sep-12 23:09
mveRichard MacCutchan23-Sep-12 23:09 
AnswerRe: Late Binding Concept PinPopular
pasztorpisti24-Sep-12 1:09
pasztorpisti24-Sep-12 1:09 
The bug in your code is obvious. CPallini already gave you the correct code but I feel this is something that needs further clarification.
The bug is the following, and its a major bug in its current context:
C++
CChildClass* pChild = (CChildClass*)pBase;

There are 2 kinds of static casts: downcast and upcast. If you are drawing the uml diagram of your class hierarchy then it looks like a tree with its root node (base class) at the top of the diagram. Down and upcast are named based on the direction of the cast in this tree so an upcast means that you cast a derived class to one of its base classes in the hierarchy, this type of cast is safe, you don't have to mark it explicitly in your code because the compiler does it automatically for you:
C++
Derived * d = new Derived;
Base* b = d;

However downcasts are not only dangerous but they usually mean that you committed a design mistake. A base class can have a lot of derived classes and if you are casting a base class to one of its derived classes than its a bug if the actual object isn't an instance of the downcast type or its descendants. People often say that dowcasts defeat the whole purpose of polymorphism/late binding. Example:
C++
class Base;
class A : public Base;
class B : public Base;
Base* b = new B;
A* p = (A*)b; // huge mistake because b points to a B instance so the code that uses p can crash anytime if you are not lucky!!!!


So why doesn't your code crash??? Because you were lucky and currently in your simple example the binary representation of instances created by the compiler for your base and derived classes are probably identical, they just differ in their vtable pointer but in any more complex situations it could easily crash.

OK, so when you have a complex hierarchy and you call a virtual function on a base class pointer then which method is executed??? It depends on which object have you actually created! (with new). Since in your example you created just a Base instance it would be magic to see executing the method of the derived class, and its just luck that using the pointer that you filled with your invalid downcast haven't crashed your program.

So based on what we have learned up until now you should only use upcasts that are done automatically for you by the compiler, for example:
C++
CBaseClass* p = new CChildClass; // the compiler automatically upcasts the CChildClass* to CBaseClass*
p->Function();                   // calls the Function of CChildClass because we created a CChildClass instance above!

Describing the same in other words:
You can always use a base class pointer to point to an object that was created by instantiating a class derived from the base class. This way if you call some virtual methods on the base class pointer then the methods of the actually instantiated class are called.

modified 24-Sep-12 14:17pm.

GeneralRe: Late Binding Concept Pin
CPallini24-Sep-12 1:41
mveCPallini24-Sep-12 1:41 
GeneralRe: Late Binding Concept Pin
pasztorpisti24-Sep-12 1:46
pasztorpisti24-Sep-12 1:46 
GeneralRe: Late Binding Concept Pin
Chris Meech24-Sep-12 2:19
Chris Meech24-Sep-12 2:19 
GeneralRe: Late Binding Concept Pin
pasztorpisti24-Sep-12 2:37
pasztorpisti24-Sep-12 2:37 
AnswerRe: Late Binding Concept Pin
Chuck O'Toole24-Sep-12 5:16
Chuck O'Toole24-Sep-12 5:16 
GeneralRe: Late Binding Concept Pin
pasztorpisti24-Sep-12 6:00
pasztorpisti24-Sep-12 6:00 
GeneralRe: Late Binding Concept Pin
pasztorpisti24-Sep-12 6:19
pasztorpisti24-Sep-12 6:19 
GeneralRe: Late Binding Concept Pin
Chuck O'Toole24-Sep-12 7:53
Chuck O'Toole24-Sep-12 7:53 
QuestionCallBack Function Pin
Smart Arab23-Sep-12 4:47
Smart Arab23-Sep-12 4:47 
AnswerRe: CallBack Function Pin
Mohibur Rashid23-Sep-12 6:06
professionalMohibur Rashid23-Sep-12 6:06 
GeneralRe: CallBack Function Pin
Smart Arab24-Sep-12 1:39
Smart Arab24-Sep-12 1:39 
QuestionQuestion about MFC implementation Pin
Jian Jun Liu23-Sep-12 2:33
Jian Jun Liu23-Sep-12 2:33 
AnswerRe: Question about MFC implementation Pin
pasztorpisti23-Sep-12 3:20
pasztorpisti23-Sep-12 3:20 
GeneralRe: Question about MFC implementation Pin
Jian Jun Liu24-Sep-12 15:01
Jian Jun Liu24-Sep-12 15:01 
GeneralRe: Question about MFC implementation Pin
pasztorpisti24-Sep-12 15:23
pasztorpisti24-Sep-12 15:23 
GeneralRe: Question about MFC implementation Pin
Jian Jun Liu26-Sep-12 16:59
Jian Jun Liu26-Sep-12 16:59 
AnswerRe: Question about MFC implementation Pin
Chris Meech23-Sep-12 4:57
Chris Meech23-Sep-12 4:57 

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.