1,
Class cbase
{
Int;
Char
* P;
}; Cout <"sizeof (cbase) =" <sizeof (cbase) <Endl; answer: 82, class cbase
{
};
Run cout <"sizeof (cbase) =" <sizeof (cbase) <Endl;
Sizeof (cbase) = 1; instantiation, the so-called class instantiation is to allocate an address in the memory, each instance has a unique address in the memory. Similarly, empty classes will also be instantiated (do not take the Bean package improperly for dry food, empty classes are also classes), so the compiler will implicitly Add a byte to the empty class, in this way, the unique address is available after the empty class is instantiated. Therefore, the empty sizeof class is 1.
3. After adding a virtual function
Class cbase
{
Public:
Cbase (void );
Virtual
~ Cbase (void );
PRIVATE:
Int;
Char
* P;
};
Run sizeof (cbase) = 12 again. Why? : "When there is a virtual function in the C ++ class, there is a pointer to the virtual function (vptr). In 32-bit systems, the pointer size is 4 bytes."
4. inherit class cChild:
Public cbase
{
Public:
CChild (void );
~ CChild (void );
PRIVATE:
Int B;
};
Run: cout <"sizeof (cChild) =" <sizeof (cChild) <Endl;
Output: sizeof (cChild) = 16;
The subclass size is the size of its member variables plus the subclass size.