Click here to Skip to main content
15,902,189 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: startup application on pocketpc Pin
toxcct15-Nov-05 1:10
toxcct15-Nov-05 1:10 
GeneralRe: startup application on pocketpc Pin
sudabeh15-Nov-05 18:09
sudabeh15-Nov-05 18:09 
QuestionVirtual constructors... Pin
funwithdolphin15-Nov-05 0:07
funwithdolphin15-Nov-05 0:07 
AnswerRe: Virtual constructors... Pin
Cedric Moonen15-Nov-05 0:45
Cedric Moonen15-Nov-05 0:45 
AnswerRe: Virtual constructors... Pin
Bob Stanneveld15-Nov-05 6:01
Bob Stanneveld15-Nov-05 6:01 
GeneralRe: Virtual constructors... Pin
toxcct15-Nov-05 6:24
toxcct15-Nov-05 6:24 
GeneralRe: Virtual constructors... Pin
Bob Stanneveld15-Nov-05 22:57
Bob Stanneveld15-Nov-05 22:57 
AnswerRe: Virtual constructors... Pin
cmk15-Nov-05 12:23
cmk15-Nov-05 12:23 
Objects are created from the inside out, and destroyed from outside in.

class A {
public:
    A( void );
    virtual ~A( void );
    virtual void F( void );
    long DA;
};
 
class B : public A {
    B( void );
    virtual ~B( void );
    virtual void F( void );
    long DB;
};
When you create an instance of A or B (or any class with a vtable) an pointer is placed at the start of the object. This pointer points to the class vtable.
e.g.
A  a;    // [4 bytes - vtable ptr A][4 bytes - DA]
B  b;    // [4 bytes - vtable ptr B][4 bytes - DA][4 bytes - DB]

When b is created:
- memory is taken from the stack to hold size B object (12 bytes)
- vtable ptr is set to A vtable
- A::A() is called
- vtable ptr is set to B vtable
- B::B() is called

Setting the vtable ptr and calling the approriate constructor method can be considered an atomic action from our point of view.

From the above you can also see why you can't call virtual methods from a constructor and have them resolve forward - the vtable matches the class of the current constructor.
e.g.
A::A( void ) {
    F();    // ALWAYS calls A::F(), will never call B:F()
}

This is part of the reason many frameworks adopt 2 part object construction:
1. create an instance, don't do anything important in constructor (init members)
2. call an Init() method that does the real construction - will resolve virtual methods properly


When b is destroyed:
- (vtable already set to B vtable)
- B::~B() is called
- vtable ptr is set to A vtable
- A::~A() is called
- memory is returned to stack

From the above you can also see why you can't call virtual methods from a destructor and have them resolve forward - the vtable matches the class of the current destructor.
e.g.
A::~A( void ) {
    F();    // ALWAYS calls A::F(), will never call B:F()
}


...cmk

Save the whales - collect the whole set
QuestionAbstract Base classes vs Interfaces Pin
funwithdolphin15-Nov-05 0:01
funwithdolphin15-Nov-05 0:01 
AnswerRe: Abstract Base classes vs Interfaces Pin
Cedric Moonen15-Nov-05 0:52
Cedric Moonen15-Nov-05 0:52 
AnswerRe: Abstract Base classes vs Interfaces Pin
deardear15-Nov-05 1:00
deardear15-Nov-05 1:00 
AnswerRe: Abstract Base classes vs Interfaces Pin
toxcct15-Nov-05 1:02
toxcct15-Nov-05 1:02 
AnswerRe: Abstract Base classes vs Interfaces Pin
LogiPro10125-Jan-09 11:32
LogiPro10125-Jan-09 11:32 
QuestionBuiding dll with Visual Studio 2003 Pin
Emb_Emb14-Nov-05 23:42
Emb_Emb14-Nov-05 23:42 
QuestionEasy installation of printer drivers Pin
Palani Surendrnath14-Nov-05 23:40
Palani Surendrnath14-Nov-05 23:40 
AnswerRe: Easy installation of printer drivers Pin
toxcct15-Nov-05 0:13
toxcct15-Nov-05 0:13 
QuestionActiveX control without parent container Pin
Ahsan Askare14-Nov-05 23:35
Ahsan Askare14-Nov-05 23:35 
QuestionEasy installation of printer drivers Pin
Palani Surendrnath14-Nov-05 23:23
Palani Surendrnath14-Nov-05 23:23 
Questionwstring to LPCTSTR Pin
Luke Murray14-Nov-05 22:54
Luke Murray14-Nov-05 22:54 
AnswerRe: wstring to LPCTSTR Pin
kakan15-Nov-05 0:07
professionalkakan15-Nov-05 0:07 
GeneralRe: wstring to LPCTSTR Pin
Luke Murray15-Nov-05 12:44
Luke Murray15-Nov-05 12:44 
AnswerRe: wstring to LPCTSTR Pin
22491715-Nov-05 1:56
22491715-Nov-05 1:56 
GeneralRe: wstring to LPCTSTR Pin
sunit515-Nov-05 2:21
sunit515-Nov-05 2:21 
GeneralRe: wstring to LPCTSTR Pin
toxcct15-Nov-05 2:43
toxcct15-Nov-05 2:43 
GeneralRe: wstring to LPCTSTR Pin
22491715-Nov-05 5:44
22491715-Nov-05 5:44 

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.