Click here to Skip to main content
16,011,120 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
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 
The binary layout of a C++ object is a structure which keeps the attributes ( in the order of definition ) and if the class inherits from some other classes the structure is prefixed with the parent vptr table and data ( in the order of definition). So, for a simple hierarchy like "class child : public parent " the layout will be:
parent::vptr_table
parent::data
child::data
and if we have a more complex hierarchy like :
class base;
class parent1 : public virtual base;
class parent2 : public virtual base;
class child : public parent1, public parent2
the layout will be:
base::vptr_table
base::data
parent1::vptr_table
parent1:data
parent2:vptr_table
parent2::data
child::data
Let’s say you have a class named CObject and you create an instance called x . Since the calling convention of a member function is the "this" convention , the compiler will just copy into the ECX register the "this" value for x object , will push the parameters on the stack and will call the method :
Lea ecx, x
Push ..
Call CObject::method
If you will define another instance of CObject called y the method call will be :
Lea ecx, y
Push ..
Call CObject::method
If you want to access the attributes just point to the class location and get the variable by offset, and to call method functions thru vptr table all you have to do is pointer arithmetic . If you have time you can check this sample :

#include <stdio.h>

class CBase1
{
public:
virtual void b1_test1() = 0;
virtual void b1_test2() = 0;
};

class CBase2
{
public:
virtual void b2_test1() = 0;
virtual void b2_test2()
{
};
virtual void b2_test3()
{
};
};

class CTest : public CBase1, public CBase2
{
public:
void b1_test1();
void b1_test2();
void b2_test1();
void b2_test3();
void b2_test2()
{
printf("b2_test2\n");
};
};

void CTest::b1_test1()
{
printf("b1_test1\n");
};

void CTest::b1_test2()
{
printf("b1_test2\n");
};

void CTest::b2_test1()
{
printf("b2_test1\n");
};

void CTest::b2_test3()
{
printf("b2_test3\n");
};


void test_vptr()
{
CTest x;

//call CBase1::b1_test1
_asm
{
lea ecx, x
mov eax, dword ptr[ ecx] //get CBase1 vptr
call dword ptr[ eax + 0] //first method from table
}

//call CBase2::b2_test3
_asm
{
lea ecx, x
mov eax, dword ptr[ ecx + 4] //get CBase2 vptr - second base class
call dword ptr[ eax + 8] //third method from table
}

//call CBase2::b2_test2
_asm
{
lea ecx, x
mov eax, dword ptr[ ecx + 4] //get CBase2 vptr
call dword ptr[ eax + 4] //second method from table
}
};

class CSimple
{
private:
int k;
int f;
public:
CSimple(int v)
{
k = v;
f = 3;
}
void test();
};

void CSimple::test()
{
printf("simple::test\n");
};

void test_simple()
{
CSimple x(1);
CSimple y(2);
int t = 0;

//printf x.k
_asm
{
mov eax, dword ptr[ x]
mov t, eax
}
printf("x::k : %d\n", t);

//printf x.f
_asm
{
mov eax, dword ptr[ x + 4]
mov t, eax
}
printf("x::f : %d\n", t);

//printf y.k
_asm
{
mov eax, dword ptr[ y]
mov t, eax
}
printf("y::k : %d\n", t);

}
void main(void)
{
test_vptr();
test_simple();
}
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 
QuestionHow to minimaze a dialog ? Pin
7-Mar-02 8:33
suss7-Mar-02 8:33 
AnswerRe: How to minimaze a dialog ? Pin
Carlos Antollini7-Mar-02 8:43
Carlos Antollini7-Mar-02 8:43 
AnswerRe: How to minimaze a dialog ? Pin
Gaurika Wijeratne7-Mar-02 9:10
Gaurika Wijeratne7-Mar-02 9:10 
QuestionMSFlexGrid GetTextArray? Pin
dazinith7-Mar-02 8:18
dazinith7-Mar-02 8:18 

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.