SIze of object

hi,

Simple que:


class Parent
{

};
class Child : virtual public Parent
{

};


what is the size of object of Class Child in following case?
Please don't cross post the same question to different forums.
Hi cire,

waiting for reply...
why don't you try it yourself? The size of an object is compiler/settings dependent
hi coder I am not intrested in size actually but yes I want to know vptr is to indicate.

because of virtual base inheritance one vptr is added in to child class and that why its size increaded by 4 byte.

whats vptr?

is it the pointer to indicate the virtual table? whose virtual table is this then?


Thanx.

But I am not asking about VPTR in case of virtual function.

I am asking VPTR in case virtual base class.

I just want to know in this case , this VPTR point to which ?

This is the general idea:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

typedef struct A { int i ; } A;

void A_constructor( void* a ) 
{ 
    A* this = a ; 
    this->i = 100 ; 
    printf( "A::A() this==%p\n", (void*)this ) ; 
}

void A_destructor( A* this ) 
{ 
    this->i = 0 ; 
    printf( "A::~A() this==%p\n", (void*)this ) ; 
}

int A_foo( A* this ) 
{ 
    printf( "A::foo() this==%p\n", (void*)this ) ; 
    return this->i ; 
}

typedef struct B /* : virtual A */ {  A* virtual_base_pointer ; int j ; } B; 

void B_constructor( void* b, void* a, bool not_further_derived ) 
{ 
    if( not_further_derived ) A_constructor(a) ;
    B* this = b ; 
    this->virtual_base_pointer = a ; 
    this->j = 7777 ; 
    printf( "B::B() this==%p\n", (void*)this ) ; 
}

void B_destructor( B* this, bool not_further_derived )
{ 
    this->j = 0 ;
    printf( "B::~B() this==%p\n", (void*)this ) ;  
    if( not_further_derived ) A_destructor( this->virtual_base_pointer ) ;
}

int B_foo( B* this ) 
{ 
    printf( "B::foo() this==%p\n", (void*)this ) ; 
    return A_foo( this->virtual_base_pointer ) + this->j ; 
}

int main()
{
    char a[ sizeof(A) ] ; // memory to place the A subobject
    char b[ sizeof(B) ] ; // memory to place B
    
    B_constructor( b, a, true ) ; // construct B
    B* pb = (void*)b ;
    puts( "-------------------" ) ;
    
    int v = B_foo(pb) ; // pb->foo() ;
    printf( "B::foo returned %d\n", v ) ; 
    puts( "-------------------" ) ;
    
    A* pa = pb ? pb->virtual_base_pointer : NULL ; // A* pa = pb ;
    printf( "A* pa == %p   B* pb = %p\n", (void*)pa, (void*)pb ) ;
    puts( "-------------------" ) ;
    
    v = A_foo( pb->virtual_base_pointer ) ; // pb->A::foo()
    printf( "A::foo returned %d\n", v ) ; 
    puts( "-------------------" ) ;
    
    B_destructor(pb,true) ; // destroy B
}

http://coliru.stacked-crooked.com/a/7793c6be1c339e63

For more information, read Stan Lippman's 'Inside the C++ Object Model'
http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545
I am asking VPTR in case virtual base class.
that a base class is virtual does not (or better just indirect) contribute to the virtual function table.

The meaning of a virtual base class is that the content appears only once in multiple inheritance.

Read this:
http://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class
Last edited on
Coder777 thank. I read this but I want to know how compiler do this?
He create vptr n this vptrt indicate to..?
I read this but I want to know how compiler do this?


The following does a decent job of visually showing what's going on:
http://www.phpcompiler.org/articles/virtualinheritance.html

One should always keep in mind, though, that this implementation is not required by the standard.
Thank fire.

I read this , I think now I am more clear on vptr in case of virtual base inheritance.

But one doubt the sequence of field in derived class ( in given link Bottom class) is depend on compiler , its not any standard of this sequence.... Is this right? Plz correct me if m wrong
Everything is dependant on the compiler.

For instance, Microsoft implements it differently: http://www.google.com/patents/US5754862
Please correct me if m wrong...

Virtual table use in following condition:-

1. If class contains Virtual funcion
In this case this vtable contain list of virtual functions in class.
And one pointer is added into each object of tht class which stores address of resp VTable.
All this done by compiler at compile time.

2. Virtual base class inheritance
In this case compiler create one VTable at the time of compilation.
This VTable stores OFFSET.
One vptr is added in object of class and it contains ?
At the time of polymorphism offset added into this vptr and appropriate value of virtual base class member get at run time.

Two doubt's:-
1. Whose address stored into vptr in 2nd case?
2. In second case if compiler added new VTable in derived class (Bottom class) then why not one extra vptr which jstores address of the this VTable.

I can understand this may be a crazy que, but plz I want to clear it with the help of u guys.
> Virtual table use in following condition:-
> ...
> 2. Virtual base class inheritance
> In this case compiler create one VTable at the time of compilation.
> This VTable stores OFFSET.

Repeat:
Everything is dependant on the compiler. For instance, Microsoft implements it differently: http://www.google.com/patents/US5754862

Repeat:
For more information, read Stan Lippman's 'Inside the C++ Object Model'
http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545

But, before you read Lippman, it may be worthwhile to read this first:
http://www.constitution.org/col/blind_men.htm
Plz can anyone clear me on my above doubt?
Ask your compiler maker. This doesn't have anything to do with the language.
Oh m very sorry guys.n thanx Jl for 6men n ele.

I just want to ensure that is m correct or not in my above lines.

If any one has problem then its k don't reply.

N cire I know its near to compiler but my doubt is related to lang only.

Thax to all.
Topic archived. No new replies allowed.