Click here to Skip to main content
15,909,466 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: close dialog? Pin
V.21-Oct-04 21:27
professionalV.21-Oct-04 21:27 
GeneralRe: close dialog? Pin
Larsson21-Oct-04 21:31
Larsson21-Oct-04 21:31 
GeneralC++ question. Pin
WREY21-Oct-04 20:16
WREY21-Oct-04 20:16 
GeneralRe: C++ question. Pin
Yulianto.21-Oct-04 22:04
Yulianto.21-Oct-04 22:04 
GeneralRe: C++ question. Pin
V.21-Oct-04 23:27
professionalV.21-Oct-04 23:27 
GeneralRe: C++ question. Pin
digwizfox22-Oct-04 11:05
digwizfox22-Oct-04 11:05 
GeneralRe: C++ question. Pin
WREY22-Oct-04 13:46
WREY22-Oct-04 13:46 
GeneralRe: C++ question. Pin
digwizfox25-Oct-04 6:32
digwizfox25-Oct-04 6:32 
Virtuality is a complex subject for those beginning with C++ but after you have developed for a few years it makes sense.

Firt, let me try to give you another perspective. When you say the ABC will never become an object, you are not entirely correct. It can never become an object by itself. When a derived class is instantiated, the base class is also instantiated. You have two completely separate objects being instantiated and therefore two constructors. On a UML object diagram, you'd show an object for the system. But really there are multiple classes within the object that can be pointed to separately. Check out this example.

Let's say you have two classes, ABC_Base and derived.

within some function, I want to instantiate the class:

ABC_Base* theObjPointer = new derived(UINT nRadius, float pi);

Now you have a pointer to the base class. The only way to call a function in the derived class is through virtual functions. In order to call a function that is only in the derived class you would have to downcast the pointer into a pointer of type derived*. You see, a pointer to this new object could be a pointer to either object. So it's not one object that you have instantiated.

Let's say you did this instead:

derived* theDerivedObjPtr = new derived(int nRadius, float pi);

Now you have a pointer to the same kind of object. But now it is a type that points to the derived class.

You could dynamically cast this pointer value into the other variable as well. Look up dynamic_cast in one of your C++ books.

In order for this to work, there must be two distinct addresses, one for each class within the object. Of course the compiler somehow builds virtual tables so that the virtual functions are linked. By the way, the constructors are never virtual. The derived class constructor is always called first and subsequently calls the constructor in the class that it inherits directly from; and on up the chain. Now the destructor should be virtual since there are different pointer types. So if you want to delete the object using the theObjPointer variable, your destructors better be virtual, otherwise you'll only really destruct the base class part of the object and not the derived class part of the object. Another example to help reinforce that you really have muliple things within the object you've constructed.

Felt like rambling for a while; so I hope I gave you another perspective you hadn't thought of.

Regards,
Shawn
GeneralRe: C++ question. Pin
WREY25-Oct-04 7:31
WREY25-Oct-04 7:31 
GeneralReading Field Pin
picasso221-Oct-04 19:58
picasso221-Oct-04 19:58 
GeneralRe: Reading Field Pin
Yulianto.21-Oct-04 22:39
Yulianto.21-Oct-04 22:39 
GeneralRe: Reading Field Pin
David Crow22-Oct-04 4:44
David Crow22-Oct-04 4:44 
Generalusing SHBrowseForFolder Pin
includeh1021-Oct-04 19:53
includeh1021-Oct-04 19:53 
GeneralRe: using SHBrowseForFolder Pin
PJ Arends21-Oct-04 20:08
professionalPJ Arends21-Oct-04 20:08 
QuestionMFC and the MAIN method??? Pin
Anonymous21-Oct-04 19:38
Anonymous21-Oct-04 19:38 
AnswerRe: MFC and the MAIN method??? Pin
Phil J Pearson22-Oct-04 0:24
Phil J Pearson22-Oct-04 0:24 
AnswerRe: MFC and the MAIN method??? Pin
David Crow22-Oct-04 4:47
David Crow22-Oct-04 4:47 
GeneralRe: MFC and the MAIN method??? Pin
dharani26-Oct-04 22:53
dharani26-Oct-04 22:53 
GeneralRe: MFC and the MAIN method??? Pin
David Crow27-Oct-04 3:11
David Crow27-Oct-04 3:11 
Questionhow to create tab control Pin
vc-programmer-21-Oct-04 19:28
vc-programmer-21-Oct-04 19:28 
QuestionWhy I get a black screen while capturing screen? Pin
Guoguor21-Oct-04 17:35
Guoguor21-Oct-04 17:35 
AnswerRe: Why I get a black screen while capturing screen? Pin
YoSilver22-Oct-04 21:35
YoSilver22-Oct-04 21:35 
GeneralOnly Dialogbox getting popped up Pin
Raghunandan S21-Oct-04 17:34
Raghunandan S21-Oct-04 17:34 
Generalgive me samples for Screen capture with GDI,Thanks Pin
Guoguor21-Oct-04 17:24
Guoguor21-Oct-04 17:24 
GeneralRe: give me samples for Screen capture with GDI,Thanks Pin
David Crow22-Oct-04 4:49
David Crow22-Oct-04 4:49 

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.