Click to See Complete Forum and Search --> : Virtual Base Class
Nowwin
March 24th, 2003, 10:35 PM
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 ?
kuphryn
March 24th, 2003, 11:23 PM
Post an example of what you think is different. Also,what is "Hybrid Inheritance?"
Kuphryn
Graham
March 25th, 2003, 03:35 AM
Hybrid inheritance == multiple inheritance (presumably).:confused:
Paul McKenzie
March 25th, 2003, 03:54 AM
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 ? Depends on the compiler. One compiler can implement virtual base classes completely different from another compiler.
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
usman999_1
March 25th, 2003, 04:22 AM
May b the OP was'nt expecting Vptr to be included in the size of the Class?????????
Nowwin
March 25th, 2003, 07:01 AM
To clarify the question I am attaching following 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.
brapler
March 25th, 2003, 08:02 AM
Originally posted by Nowwin
To clarify the question I am attaching following 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 ?
Nowwin
March 25th, 2003, 10:24 PM
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 ?
Paul McKenzie
March 26th, 2003, 01:37 AM
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 ? 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.
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
galathaea
March 26th, 2003, 03:20 AM
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.
galathaea
March 26th, 2003, 03:22 AM
Oh yeah.
And virtual, like static, is a highly overloaded word in c++, which can often be confusing.
Gabriel Fleseriu
March 26th, 2003, 04:23 AM
Have a look at this 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;
}
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.
Also note the line I commented out. Remove the comment and replace "???" by a numerical value and see what the compiler sais.
Nowwin
March 30th, 2003, 09:55 PM
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.
Gabriel Fleseriu
March 31st, 2003, 01:19 AM
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.
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.