|
-
March 24th, 2003, 11:35 PM
#1
Virtual Base Class
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 ?
-
March 25th, 2003, 12:23 AM
#2
Post an example of what you think is different. Also,what is "Hybrid Inheritance?"
Kuphryn
-
March 25th, 2003, 04:35 AM
#3
Hybrid inheritance == multiple inheritance (presumably).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 25th, 2003, 04:54 AM
#4
Re: Virtual Base Class
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
-
March 25th, 2003, 05:22 AM
#5
May b the OP was'nt expecting Vptr to be included in the size of the Class?????????
Last edited by usman999_1; March 25th, 2003 at 05:45 AM.
-
March 25th, 2003, 08:01 AM
#6
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.
-
March 25th, 2003, 09:02 AM
#7
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.
So, what's the difference between appling the virtual or not ?
Speed up! Speed up! Speed up!
It can be faster!!!
-
March 25th, 2003, 11:24 PM
#8
Virtual Base Class
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 ?
-
March 26th, 2003, 02:37 AM
#9
Re: Virtual Base Class
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
-
March 26th, 2003, 04:20 AM
#10
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.
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
March 26th, 2003, 04:22 AM
#11
Oh yeah.
And virtual, like static, is a highly overloaded word in c++, which can often be confusing.
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
March 26th, 2003, 05:23 AM
#12
Have a look at this code:
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.
-
March 30th, 2003, 10:55 PM
#13
Virtual Base Class
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.
-
March 31st, 2003, 02:19 AM
#14
Re: Virtual Base Class
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|