Class Size Test (empty class, single inheritance with static and non-contained, virtual and non-virtual, and multi-inheritance)

Source: Internet
Author: User

 

This article mainly tests the class size, including empty classes, static and non-static variables, virtual and non-virtual single inheritance and multi-inheritance classes, and class or object

 

Test whether the virtual table and virtual pointer are the same, and give a simple explanation. If there is anything inappropriate, please point out that we will exchange and make progress together!

 

 

 

/*************************************** *****************************

** Created by Cai guowu

** Date: 2009/5/22

** Version 1.0

** Description: test the class size and explain the reason: NULL Class, Class with static or virtual table, inheritance, virtual inheritance, and other tests

Conclusion:

1: The empty class size is 1, not 0. The reason is very simple. If it exists, it will occupy space.

2: Virtual tables and virtual functions: A class corresponds to a virtual table, and each object contains a virtual function pointer (_ vptr) pointing to the virtual table.

 

3: verified: whether virtual inheritance and nested virtual inheritance can also share the memory mechanism and other tests

 

** Application:

********** ********************

** Modifier:

** Date:

** Description:

**************************************** ****************************/

Namespace testSizeOfClass

{

/////// The size of the test class start ////////

Class CEmptyClass

{

};

// Sizeof (CEmptyClass) = 1 = "The Empty class size is 1, not 0. The reason is very simple. If it exists, it will occupy space.

 

 

Class

{

Virtual void (){}

};

// Sizeof (A) = 4 // indicates A class with A virtual table pointer, which occupies 4 bytes.

 

 

 

Class A1

{

Virtual void (){}

};

// Sizeof (A1) = 4 // indicates a class with a virtual table pointer, which occupies 4 bytes.

 

 

 

Class A2

{

Static int m_sNum;

};

// Sizeof (A2) = 1 // = "static variables do not belong to a class and are shared

 

 

 

Class A3

{

Virtual void (){}

Static int m_sNum;

};

// Sizeof (A3) = 4 // = "static variables do not belong to a class, but virtual pointers consume 4 bytes

 

 

 

Class A4

{

Bool m_bValue;

Int m_nValue;

Static int m_sNum;

};

// Sizeof (A4) = 8 // Note: Memory alignment

 

 

 

 

Class B: public

{

};

// Sizeof (B) = 4

 

Class B1: virtual public A // single virtual inheritance

{

};

// Sizeof (B2) = 8 // Note: a virtual pointer is added for virtual inheritance.

 

// If you do not take the test, perform more inheritance:

// The size of B and B1, = "virtual inheritance is four bytes larger than non-virtual inheritance, which is actually a virtual pointer, continue Test

 

 

 

Class B2: public A, public A1 // multiple inheritance parent classes are all with virtual classes

{

};

// Sizeof (B2) = 4

 

Class B3: public A1, public A2 // multiple inheritance parent classes only have one with virtual class

{

};

// Sizeof (B3) = 4 // Note: a virtual pointer is added for virtual inheritance.

 

// If you do not take the test, perform more inheritance:

// The size of B, B2, and B3, =, so a class has only one virtual table and has nothing to do with multi-inheritance. An object corresponds to only one virtual pointer.

// Continue the test below.

 

 

 

Class B4: public A, virtual public A1 // multi-inheritance, single-virtual inheritance

{

};

// Sizeof (B4) = 12 // 12 = sizeof (A) + sizeof (A1) + virtual inheritance A virtual table pointer = 4 + 4 + 4

 

Class B5: public virtual A, virtual public A1 // All are virtual inheritance

{

};

// Sizeof (B5) = 12 // 12 = sizeof (A) + sizeof (A1) + virtual inheritance A virtual table pointer = 4 + 4 + 4

 

// Virtual Multi-inheritance:

// The size of B4 and B5, = as long as it is virtual inheritance, each virtual inheritance and multiple virtual inheritance, only four bytes are added, that is, a virtual table pointer

 

 

 

 

 

 

 

// Class C: public A1, virtual public A1 // error. The same class can only have one inheritance method.

//{

//};

 

 

 

 

 

Class C1: public B, virtual public

{

};

// Sizeof (C) = 12

 

 

 

Class C2: public B1, virtual public

{

};

// Sizeof (C2) = 8 // Note: virtual inheritance can share memory, so class C2: public B1, virtual public A is equivalent to class C2: public B1

 

 

Class C3: public B1 // verification: Virtual inheritance can share the memory mechanism, because B1 already has virtual inheritance

{

};

// Sizeof (C3) = 8 // Note: Virtual inheritance can share memory.

 

 

 

Class C4: virtual public B1 // because B1 already has virtual inheritance

{

};

// Sizeof (C4) = 12 // Note: Virtual inheritance can share memory.

 

 

 

 

 

// Verify: whether the virtual inheritance nesting mechanism can also share the memory, because the virtual pointer of virtual B1 also points to its own virtual inherited

 

Class C5: virtual public B1, virtual public

 

{

};

// Sizeof (C5) = 12 // Note: virtual inheritance can share memory, so class C2: public virtual B1, virtual public A is equivalent to class C2: virtual public B1

 

 

 

 

 

 

 

 

////// Size of the test class end ////////

 

 

Void testSizeof (void)

{

A a1, a2;

 

// Virtual table pointers of different objects of the same class are the same. The test is as follows:

If (& a1 = & a2) // This is true

{

Cout <"virtual table pointers vptr of different objects of the same class are the same ";

}

Else

{

Cout <"virtual table pointer vptr of different objects of the same class is different ";

}

 

 

Int nSizeof;

CEmptyClass

NSizeof = sizeof (CEmptyClass );

 

NSizeof = sizeof ();

NSizeof = sizeof (A1 );

NSizeof = sizeof (A2 );

NSizeof = sizeof (A3 );

NSizeof = sizeof (A4 );

NSizeof = sizeof (B );

NSizeof = sizeof (B1 );

NSizeof = sizeof (C );

NSizeof = sizeof (C1 );

NSizeof = sizeof (C2 );

NSizeof = sizeof (C3 );

NSizeof = sizeof (C4 );

NSizeof = sizeof (C4 );

}

}

 

Author: caiguowu

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.