How is the size of an object (of the class created by using Hybrid Inheritance) calculated ?
When I saw the sizeof the object involving Virtual Base Classes, it was different then what I expected. What exactly is the logic ?
Printable View
How is the size of an object (of the class created by using Hybrid Inheritance) calculated ?
When I saw the sizeof the object involving Virtual Base Classes, it was different then what I expected. What exactly is the logic ?
Post an example of what you think is different. Also,what is "Hybrid Inheritance?"
Kuphryn
Hybrid inheritance == multiple inheritance (presumably).:confused:
Depends on the compiler. One compiler can implement virtual base classes completely different from another compiler.Quote:
Originally posted by Nowwin
When I saw the sizeof the object involving Virtual Base Classes, it was different then what I expected. What exactly is the logic ?
Your best bet into trying to figure it out is to actually get the specs for the compiler and its VBC implementation. I believe that some compiler vendors supply this information.
Regards,
Paul McKenzie
May b the OP was'nt expecting Vptr to be included in the size of the Class?????????
To clarify the question I am attaching following code:
Code:class A
{
public :
int a;
int b;
} ;
class B : public virtual A
//class B : public A
{
public :
int a;
} ;
//class C : public virtual A
class C : public A
{
public :
int a;
} ;
class HYBRID : public B,C
{
public :
int a;
} ;
void main ()
{
HYBRID obj ;
cout << sizeof (obj) ;
}
Uncomment the lines and see the changing results.
Hope u got it.
So, what's the difference between appling the virtual or not ?Quote:
Originally posted by Nowwin
To clarify the question I am attaching following code:
Code:class A
{
public :
int a;
int b;
} ;
class B : public virtual A
//class B : public A
{
public :
int a;
} ;
//class C : public virtual A
class C : public A
{
public :
int a;
} ;
class HYBRID : public B,C
{
public :
int a;
} ;
void main ()
{
HYBRID obj ;
cout << sizeof (obj) ;
}
Uncomment the lines and see the changing results.
Hope u got it.
I want you to notice the difference in the size of the object (by uncommenting the commented line, and commenting the uncommented line).
i.e First keep the code as it is and see the output. Then uncomment the commented lines and comment the uncommented lines, for e.g.
original code :
class B : public virtual A
//class B : public A
{
....
} ;
new code:
//class B : public virtual A
class B : public A
{
....
} ;
The output differs.
You can try out these combinations:
1. B,C both deriving from virtual base class.
2. Only B deriving from virtual base class.
3. Only C deriving from virtual base class.
4. No class deriving from virtual base class.
There is change in output. Why the change? What is the logic?
Is there any extra field (like vptr in case of virtual functions) kept by the compiler ?
Maybe there are 1, 2, 3, or a whole set of information kept by the compiler. Like I stated, no one, unless they have documentation from Microsoft, can be 100% sure how this is implemented. I believe there are also implementations of VBC's where the data is not stored in contiguous memory, so doing a memcpy on an instance of a VBC will screw up things totally.Quote:
Originally posted by Nowwin
There is change in output. Why the change? What is the logic?
Is there any extra field (like vptr in case of virtual functions) kept by the compiler ?
Forgive me for asking, but why is it important for you to know what the sizeof a derived virtual base class is?
Regards,
Paul McKenzie
These classes will have no vtable or other internal implementation of virtualization, as there are no virtual methods. Instead, they use virtual inheritence, which can be thought of as the "singleton of inheritence". If a class is not declared as being virtually inherited, diamond inheritence will cause multiple copies of the state members of the mutual base in the derived, which can be resolved through scoping through the intermediate discriminating classes. Adding virtual relieves the necessity for disambiguation between paths as only one copy of the mutually inherited class's state members are stored in the derived.
Oh yeah.
And virtual, like static, is a highly overloaded word in c++, which can often be confusing.
Have a look at this code:
D has two base classes of type A (indirect, one thru B and one thru C) while E has only one base class of type A because of the virtual inheritance. This, and the presence of a vtable (compiler dependent) will make out the difference of size between D and E.Code:class A
{
public:
A(int i):i_(i){}
int i_;
};
class B: public A
{
public:
B(int i):A(i){}
};
class C: public A
{
public:
C(int i):A(i){}
};
class vB: public virtual A
{
public:
vB(int i):A(i){}
};
class vC: public virtual A
{
public:
vC(int i):A(i){}
};
class D: public B, public C
{
public:
class D(int i):B(i), C(i){}
};
class E: public vB, public vC
{
public:
E(int i):A(i), vB(i), vC(i){}
};
int main(int argc, char* argv[])
{
D d(0);
d.B::i_ = 0;
d.C::i_ = 1;
// d.i_ = ??? <--- error i_ is ambiguous
E e(0);
e.i_ = 0;
cout<<sizeof(D)<<endl;
cout<<sizeof(E)<<endl;
return 0;
}
Also note the line I commented out. Remove the comment and replace "???" by a numerical value and see what the compiler sais.
Thank You.
But still, can anybody guess as to how the virtual base classes are implemented? Atleast a way in which it can be implemented.
Thanks once again for such prompt replies. It is turning out to be a nice discussion.
You shouldn't care about that if you don't want to write a C++ compiler. Even if you knew how a specific compiler implements virtual base classes, using this knowledge in your code would be a non-portable hack.Quote:
Originally posted by Nowwin
But still, can anybody guess as to how the virtual base classes are implemented? Atleast a way in which it can be implemented.