"Deep understanding C + + object Model" Reading notes (i) __c++

Source: Internet
Author: User

1, C + + class object model includes Non-static member variables and virtual function table pointers, other static member variables and member functions are placed outside the object model, all object examples can be used together. This can save access time and space efficiency.


2, virtual inheritance is to solve the occurrence of multiple inheritance in the common base class appears two semantic.

Class A;

Classb1:public virtual A;

Classb2:public virtual A;

Classd:public B1,public B2;

3, virtual functions to achieve polymorphism, that is, through the base class pointers to access other derived classes in the same name function. Implement dynamic Run-time binding.

4. Transformation cast cannot change the true address contained in the pointer, it only affects the memory size and its contents of the indicated address. For example, the null pointer is transformed into another pointer.

5, for a class, if there is no user-defined constructor, the compiler will secretly generate a default constructor, there are four main cases: A: Member class object with default constructor; B: base class with default constructor; C: Class with virtual functions, primarily to correctly initialize values of virtual function tables; D: Containing has a virtual base class that handles a pointer to an operation's virtual base class.

There are several principles: 1, if the class display defines any constructors, then the compiler does not generate a default constructor, 2, compiler-synthesized default constructor, and does not explicitly give each data member in the Class A default value.

6. A copy constructor is often used to assign the value of an object to another object, with three cases: 1, initializing an object with another object, 2, passing the object as a parameter to a function, and 3, the return value of the object as a function.

In four cases, the compiler is required to produce a copy constructor:


The latter two cases are mainly due to the existence of virtual function table pointers to consider.

7. Initialization list: Initialization order is only related to the order of Declaration. Taking full advantage of the initialization list can improve the efficiency of the program.

8, the function of overloading (overload), overlay (override), shadowing: overloading occurs between different member functions in the same class, with the same name, with a different parameter type or number; Overrides are also called Overrides, which is worth being between a base class and a derived class, a virtual function of a base class with the same name as a derived class with the same parameters Number rewrite, overwrite, hide also refers to the base class and derived classes, but the parameters of the same name but different functions, if the same name and the same argument, and the base class without virtual keywords, then the base class functions are quilt class hidden.

9, the size of the empty class is 1 bytes, the compiler needs to insert a char byte in the class so that the class has a unique address in memory.

10. The size of the class is related to the following factors: non-static member variables, virtual inheritance, non-static member variables of the base class.

11. Elements in the same access level (Public/private/protect) are arranged in memory in the same order as the declaration order,

12. Access to static member variables is the same through pointer and object access, but for non-static member variables, it needs to be accessed by obtaining an offset from the starting position. Virtual inheritance causes access to member variables to be increased indirectly and less efficient.


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.