How to understand the virtual table and its calculation of the size of the class containing the virtual function

Source: Internet
Author: User

Under what circumstances will the system automatically generate default (default) constructors?

1, in the class has a class type of object, this class has its own default constructor (with default parameters, parameters have default values).

Class B

{

B (int data)

{  }

}; cannot be synthesized

Class B

{

B (int data=0)

{  }

}; can be synthesized

Class B

{

Public

B (int data=0)

{  }

};

Class C:public B

{

Public

C ()

: B (0)

{  }

b b; The compiler will automatically synthesize

};

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/45/wKioL1cYizySzPe7AAAEzKZYT74235.png "title=" Virtual base class picture 1.png "alt=" Wkiol1cyizyszpe7aaaezkzyt74235.png "/>

2. Class is virtual inheritance

Class B

{

Public

B ()

{}

};

Class D:virtual Public B

{

Public

D ()

{ }

b b;

};

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/45/wKioL1cYi4DQ6hQFAAAFyW4Ryr4745.png "title=" Virtual base class 2.png "alt=" Wkiol1cyi4dq6hqfaaafyw4ryr4745.png "/>

3. The base class has a default constructor, and the subclass does not display the constructor that defines itself

Class B

{

Public

B (int data=0)

{}

};

Class D:public B

{

Public

};

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7F/47/wKiom1cYiv7Rp83dAAAE1JkXNHg716.png "title=" Virtual base class 3.png "alt=" Wkiom1cyiv7rp83daaae1jkxnhg716.png "/>


4, a class with virtual functions, the system automatically generates a default constructor to initialize the virtual pointer

Class B

{

Public

virtual void Fun ()

{

cout << "Fun" << Endl;

}

};

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/47/wKiom1cYizXz8VDFAAAEeyrg3_Q065.png "title=" Virtual base class 4.png "alt=" Wkiom1cyizxz8vdfaaaeeyrg3_q065.png "/>

The front is some of the structural functions of the summary, rookie a write is too bad, come in first make up to see, will not be timed to add, there is a wrong place also please more guidance

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

virtual function:

Class Base

{

Public

virtual void Funtest ()

{

cout << "Base::funtest ()" << Endl;

}

virtual void FunTest1 ()

{

cout << "Base::funtest1 ()" << Endl;

}

virtual void FunTest2 ()

{

cout << "Base::funtest2 ()" << Endl;

}

virtual void FunTest3 ()

{

cout << "Base::funtest3 ()" << Endl;

}

virtual void FunTest4 ()

{

cout << "Base::funtest4 ()" << Endl;

}

};

int main ()

{

Base b;

System ("pause");

return 0;

}

Base b;

009253E8 Lea Ecx,[b]

009253EB call Base::base (0921226h)

There is a call command stating that the compiler has a composite default constructor

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/45/wKioL1cYjVCCj6QWAAAX-esunM8894.png "title=" Virtual base class 5.png "alt=" Wkiol1cyjvccj6qwaaax-esunm8894.png "/>

Take the address of B, where B points to a space where there is another address, where the address points to the space where the virtual table address is stored

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7F/47/wKiom1cYjKeT6MhgAAAG15Bnopg852.png "style=" float: none; "title=" Virtual base class 6.png "alt=" Wkiom1cyjket6mhgaaag15bnopg852.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/45/wKioL1cYjWqyYsLbAAAMCh6wrXg205.png "style=" float: none; "title=" Virtual base class 7.png "alt=" Wkiol1cyjwqyyslbaaamch6wrxg205.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/47/wKiom1cYjMLy2QiKAABRxxzPgMI347.png "style=" float: none; "title=" Virtual base class 8.png "alt=" Wkiom1cyjmly2qikaabrxxzpgmi347.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/47/wKiom1cYjMKQ8lkdAAAwFrykXRA871.png "style=" float: none; "title=" Virtual base class 9.png "alt=" Wkiom1cyjmkq8lkdaaawfrykxra871.png "/>

Virtual pointer pointing to virtual table

The order of the virtual table is the same as the declaration order of the function

The size of the virtual function:

Calculate Size *********************************

Class C

{

Public

Char A; 1

static char B; Static members are in the static area, not in the stack area, so not counted

void *p; Pointer takes 4 bytes

static int *c; Static members are in the static area, not in the stack area, so not counted

virtual void func1 ()

{}

virtual void Func2 ()

{}

Virtual functions belong to the same class, so just one pointer to the virtual function table,

Consumes 4 bytes, even if there are n virtual functions, 4 bytes

};


int main ()

{

c C;

cout << sizeof (C) << Endl; 12

cout << sizeof (C.A) << Endl; 1

cout << sizeof (C.B) << Endl; 1 Types of size

cout << sizeof (C.C) << Endl; 4 Types of size

cout << sizeof (C.P) << Endl; 4

return 0;

}


How to understand the virtual table and its calculation of the size of the class containing the virtual function

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.