Click here to Skip to main content
16,010,918 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: efficient sorting [was: STL iterator::operator+()] Pin
Joaquín M López Muñoz7-Mar-02 10:02
Joaquín M López Muñoz7-Mar-02 10:02 
GeneralRe: efficient sorting [was: STL iterator::operator+()] Pin
moredip7-Mar-02 10:15
moredip7-Mar-02 10:15 
GeneralRe: efficient sorting [was: STL iterator::operator+()] Pin
Tomasz Sowinski7-Mar-02 23:52
Tomasz Sowinski7-Mar-02 23:52 
GeneralVC++ object layout Pin
Joaquín M López Muñoz7-Mar-02 9:20
Joaquín M López Muñoz7-Mar-02 9:20 
GeneralRe: VC++ object layout Pin
Serge Krynine7-Mar-02 11:32
Serge Krynine7-Mar-02 11:32 
GeneralRe: VC++ object layout Pin
Joaquín M López Muñoz7-Mar-02 11:35
Joaquín M López Muñoz7-Mar-02 11:35 
GeneralRe: VC++ object layout Pin
Serge Krynine7-Mar-02 11:57
Serge Krynine7-Mar-02 11:57 
GeneralRe: VC++ object layout Pin
Tim Smith7-Mar-02 12:53
Tim Smith7-Mar-02 12:53 
Hmm, here is a quick run down...

Like a structure, data inside a class is placed inside the class in the order it is defined using the current alignment. (99% sure...)

The virtual table is in the order that virtual functions are defined. (I think...)

The virtual table is a simple pointer to a table at the beginning of the class.

If a class is derived from another class, then any virtual functions defined in the derived class are added at the end of the vtable (I think, that is most logical). Overridden methods just replace the proper entry in the vtable.

All objects of the same class share the same vtable. However, a base class of a derived class might not share the same vtable as an instance of the base class when it is created directly (i.e. not derived).

HERE IS WHERE IT GET HAIRY...

When class is derived from 2 or more classes that contain virtual tables, then there actually exists multiple virtual tables in the object. Let us take an example where class C is derived from A and B in that order. At the head of object C is the vtable for all the virtual methods in A, B and C. At the start of B you have the vtable for B (which might contain overridden methods in C.)

Now the fun begins with the required thunking of the this pointer.

First, let us consider the case where the program has a pointer to object C and wants to invoke a method in B that isn't overridden by C. The calling routine would access the vtable at the start of C (which is the combo A, B and C vtable) and invoke that routine using C as the address. However, since the routine has NOT been overridden, the method in B would be receiving a pointer to C while expecting a pointer to B. So instead of having a pointer to the method in B in the combo vtable, you actually have a pointer to a thunk routine that adjusts the this pointer from C to B and then invokes the real routine.

In the opposite case, you have an object C which a program is referencing via a pointer to B. The program wishes to invoke a method in B which has been overridden by C. When the method is invoked, the vtables at the start of the B object inside of C is accessed (since all you have is a pointer to B, you are required to access its vtable). The method address retrieved from the vtable is actually the pointer to the overridden method in C. But, like in the previous case if we passed this, which is pointing to B to the routine expecting a pointer to C, the program would fail. Thus once again the vtable points to a thunk that adjusts the this pointer and then invokes the proper routine.

Smile | :)

(I hope I got most of this right...)

Tim Smith

I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
GeneralRe: VC++ object layout Pin
Serge Krynine7-Mar-02 12:56
Serge Krynine7-Mar-02 12:56 
GeneralRe: VC++ object layout Pin
Tim Smith7-Mar-02 14:57
Tim Smith7-Mar-02 14:57 
GeneralRe: VC++ object layout Pin
Nemanja Trifunovic7-Mar-02 13:01
Nemanja Trifunovic7-Mar-02 13:01 
GeneralRe: VC++ object layout Pin
pba_7-Mar-02 17:00
pba_7-Mar-02 17:00 
GeneralThank you all Pin
Joaquín M López Muñoz7-Mar-02 19:51
Joaquín M López Muñoz7-Mar-02 19:51 
GeneralRe: Thank you all Pin
Tim Smith8-Mar-02 2:02
Tim Smith8-Mar-02 2:02 
QuestionArray of objects? Pin
clintsinger7-Mar-02 8:40
clintsinger7-Mar-02 8:40 
AnswerRe: Array of objects? Pin
Joaquín M López Muñoz7-Mar-02 8:51
Joaquín M López Muñoz7-Mar-02 8:51 
GeneralRe: Array of objects? Pin
clintsinger7-Mar-02 10:25
clintsinger7-Mar-02 10:25 
GeneralRe: Array of objects? Pin
Joaquín M López Muñoz7-Mar-02 10:59
Joaquín M López Muñoz7-Mar-02 10:59 
AnswerRe: Array of objects? Pin
HomeNuke7-Mar-02 8:55
HomeNuke7-Mar-02 8:55 
GeneralRe: Array of objects? Pin
clintsinger7-Mar-02 9:46
clintsinger7-Mar-02 9:46 
GeneralRe: Array of objects? Pin
HomeNuke7-Mar-02 9:48
HomeNuke7-Mar-02 9:48 
GeneralRe: Array of objects? Pin
Joaquín M López Muñoz7-Mar-02 9:53
Joaquín M López Muñoz7-Mar-02 9:53 
GeneralRe: Array of objects? Pin
clintsinger7-Mar-02 10:13
clintsinger7-Mar-02 10:13 
GeneralRe: Array of objects? Pin
Jon Hulatt7-Mar-02 22:03
Jon Hulatt7-Mar-02 22:03 
GeneralRe: Array of objects? Pin
Jon Hulatt7-Mar-02 22:04
Jon Hulatt7-Mar-02 22:04 

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.