virtual and sizeof

May 4, 2009 at 9:16am
class A{
virtual void foo(){};
};sizeof(A)=1

class B : public virtural A{
virtual void foo(){};
};
sizeof(B)=? and why
May 4, 2009 at 9:54am
May 4, 2009 at 12:17pm
Undefined by the standard, IIRC (even though the tutorial leads you to believe otherwise).



May 4, 2009 at 8:03pm
1
2
3
class A{
virtual void foo(){};
};sizeof(A)=1 //Why do you think sizeof A  will be 1   ?? 
May 5, 2009 at 1:25am
sorry for my thoughtless, sizeof(A)=4.

Actually, I am confused by:
class C : public A{
virtual void foo(){};
};

how about sizeof(C)=?
what's the difference between B and C?
May 5, 2009 at 12:02pm
read
inside c++ object model

and clear all doubts.. :)
May 5, 2009 at 4:43pm
The difference between Class B and Class C is in the way they inherit from Class A.

class A

class B : public virtual A

class C : public A

The important bit is that word virtual it adds an extra virtual function pointer to Class B.
So you will probably find Class A is size 4, Class B is size 8 and Class C is size 4
May 6, 2009 at 2:17am
guestgulkan,

I am afraid that sizeof(B)=4.
May 8, 2009 at 4:18pm
Compiler specific?

4 8 4 microsoft;
4 4 4 mingw_g++
Topic archived. No new replies allowed.