Polymorphism in C++
-
PROBLEM Unable to understand the variation in size of object All the code is tested under Borland C++ ver 3.1 In borland V3.1 the int takes two bytes Lets look at the following code
Class base { public: int b; base() {} virtual void f() {} virtual void g() {} }; Class derive : virtual public base { public: int d; derive() {} virtual void f() {} virtual void g() {} };
……………… derive d; cout<gives 12 why? /* But if I don’t give constructors in derive then the size of derive class object is 10, because there will be no 2 bytes extra in object layout as shown below. Please help me out about finding the myth of these extra two bytes. */Object-Layout V-TABLE |------------| |------------| ---|ptr to base | -->|derive::f | | |------------| | |------------| | |derive v-ptr|-------| |derive::k | | |------------| |------------| | |d | ->|derive::f | | |------------| | |------------| | |0 |<-- | |base::g | | |------------| | | |------------| -->|base-vptr |---|----| |------------| | |b | | |------------| |**what the hell is this**
Please reply at: Contact: informfarid@yahoo.com ------------------------------------------------------------ In the middle of dificulty lies oppurtunity.
-
PROBLEM Unable to understand the variation in size of object All the code is tested under Borland C++ ver 3.1 In borland V3.1 the int takes two bytes Lets look at the following code
Class base { public: int b; base() {} virtual void f() {} virtual void g() {} }; Class derive : virtual public base { public: int d; derive() {} virtual void f() {} virtual void g() {} };
……………… derive d; cout<gives 12 why? /* But if I don’t give constructors in derive then the size of derive class object is 10, because there will be no 2 bytes extra in object layout as shown below. Please help me out about finding the myth of these extra two bytes. */Object-Layout V-TABLE |------------| |------------| ---|ptr to base | -->|derive::f | | |------------| | |------------| | |derive v-ptr|-------| |derive::k | | |------------| |------------| | |d | ->|derive::f | | |------------| | |------------| | |0 |<-- | |base::g | | |------------| | | |------------| -->|base-vptr |---|----| |------------| | |b | | |------------| |**what the hell is this**
Please reply at: Contact: informfarid@yahoo.com ------------------------------------------------------------ In the middle of dificulty lies oppurtunity.
Can I ask why you need to know? A compiler is at liberty to decide how to layout a class - the C++ standard doesnt define this. I recall finding a description of different methods on the net, so you should be able to find the details from google. I would imagine the extra byte is for some internal use by the Borland compiler, or maybe even just for padding. Either way, you shouldnt base code upon the size of an object, as compilers will give different sizes.
-
Can I ask why you need to know? A compiler is at liberty to decide how to layout a class - the C++ standard doesnt define this. I recall finding a description of different methods on the net, so you should be able to find the details from google. I would imagine the extra byte is for some internal use by the Borland compiler, or maybe even just for padding. Either way, you shouldnt base code upon the size of an object, as compilers will give different sizes.
2 extra bytes meanse shortage of ram Even if there is know reason one should know the myth of this mystry ------------------------------------------------------------You miss 100% of shots that u dont take.
-
PROBLEM Unable to understand the variation in size of object All the code is tested under Borland C++ ver 3.1 In borland V3.1 the int takes two bytes Lets look at the following code
Class base { public: int b; base() {} virtual void f() {} virtual void g() {} }; Class derive : virtual public base { public: int d; derive() {} virtual void f() {} virtual void g() {} };
……………… derive d; cout<gives 12 why? /* But if I don’t give constructors in derive then the size of derive class object is 10, because there will be no 2 bytes extra in object layout as shown below. Please help me out about finding the myth of these extra two bytes. */Object-Layout V-TABLE |------------| |------------| ---|ptr to base | -->|derive::f | | |------------| | |------------| | |derive v-ptr|-------| |derive::k | | |------------| |------------| | |d | ->|derive::f | | |------------| | |------------| | |0 |<-- | |base::g | | |------------| | | |------------| -->|base-vptr |---|----| |------------| | |b | | |------------| |**what the hell is this**
Please reply at: Contact: informfarid@yahoo.com ------------------------------------------------------------ In the middle of dificulty lies oppurtunity.
I think it's related to your use of virtual inheritance. What's your reason for making the inheritance relationship 'virtual'? Paul
-
2 extra bytes meanse shortage of ram Even if there is know reason one should know the myth of this mystry ------------------------------------------------------------You miss 100% of shots that u dont take.
Saving 2 bytes! You must be kidding - for a 1 000 instances of the class, this means (less than) a meagre 2 kbs of wasted space. This is not uncommon - "wasting" memory, that is -
struct
s will be padded, for example, for reasons of performance. You should just leave this to the compiler-writers, madness lies in this direction :-) -
PROBLEM Unable to understand the variation in size of object All the code is tested under Borland C++ ver 3.1 In borland V3.1 the int takes two bytes Lets look at the following code
Class base { public: int b; base() {} virtual void f() {} virtual void g() {} }; Class derive : virtual public base { public: int d; derive() {} virtual void f() {} virtual void g() {} };
……………… derive d; cout<gives 12 why? /* But if I don’t give constructors in derive then the size of derive class object is 10, because there will be no 2 bytes extra in object layout as shown below. Please help me out about finding the myth of these extra two bytes. */Object-Layout V-TABLE |------------| |------------| ---|ptr to base | -->|derive::f | | |------------| | |------------| | |derive v-ptr|-------| |derive::k | | |------------| |------------| | |d | ->|derive::f | | |------------| | |------------| | |0 |<-- | |base::g | | |------------| | | |------------| -->|base-vptr |---|----| |------------| | |b | | |------------| |**what the hell is this**
Please reply at: Contact: informfarid@yahoo.com ------------------------------------------------------------ In the middle of dificulty lies oppurtunity.
It may have everything to do with keeping VTables on 4-byte boundaries. Since VTables will resolve into pointers to functions in derived classes, they want to keep them aligned so the performance on the CPU is better. As an experiment, you can try changing public: int b; base() into public: byte b; base() And you will still probably get 12. Then try public: int b; int c; base() And you might still get 12. I think it has do do with optimizing pointers to tables of function pointers, myself.