C + + experience clauses

Source: Internet
Author: User

Experience on C + +:

Never let a constructor be called a virtual function:

From the simplest thought, the C + + object model is based on the virtual function table to manage the virtual function, then when the virtual function is called, it is necessary to find the virtual function table, when the object is not created, there is no virtual function table pointer, the constructor is constructed object, it is unreasonable to look for the virtual function table when object is not created successfully

In general, the compiler generates a public default constructor for each class, but there are two special case exceptions:

A class shows that a constructor is declared, in which case the compiler does not generate a public default constructor, and if the program requires a default constructor, it needs to be presented by the programmer.

A class declares a non-public constructor, and the compiler does not generate a publicly owned default constructor.

If the class has inheritance, then the construction of the base class is completed first, and then the inheritance class is constructed, and in the destruction, the order is reversed.

If the base class has virtual functions, then the creation of a virtual function table is also required in the construction of the inheriting class.

About the call to copy constructors:

When an object of a class goes to initialize another object of the class, the copy constructor is called.

If the function's formal parameter is an object of the class, the copy constructor is called when the function is combined with the parameter and the argument.

If the return value of the function is a class object, the copy constructor is called when the function call finishes returning.

Avoid calling virtual functions in a construct/destructor:

The constructor of the base class is called before the derived class constructor executes, so the data members of the derived class are not initialized when the base class constructor is run.

The call mechanism of a virtual function is completely controlled by the base class, so if the base class is not constructed, the call mechanism of the virtual function (both virtual function table) is not initialized, and all expected operations implemented by the virtual function will fail.

The size of the class object is related to what:

If there is no data member in a class, the class has a large hour of 1.

The size of an object in class is only related to the size of the object's data member, and it has nothing to do with the object's function member.

Class object non-static data member occupies memory size affects object size

The memory alignment policy used by the class object affects the size of the class object

Data members in a class object do not affect object size

The virtual function in class affects the size of the class object because the virtual function takes up 4 bytes of space for Virtual-talbe

About virtual inheritance affects object size:

Because the virtual function table and the virtual base table are involved, the vfptr pointer points to the virtual function table vftable and a vbptr pointer to the virtual base table Vbtable, which takes up a space of 8 (or 8 times the number of parent classes at multiple inheritance). Remember that there are separate virtual function table pointers and virtual base class pointers, all of which share virtual base class pointers.

The size impact factor of a class object does not include static data members, because the static data members in the fatigue are distributed across the global storage area without occupying the space of the class object.

Operator Overloading:

Do not reload && and | | Operators, because overloading && and | | |, will cause && and | | Loss of short evaluation function.

If an overloaded operator is a class member, it is called only if the left operand that is used with it is the object of that class. If the left operand of the operator must be a different type, the overloaded operator must be a namespace member.

Public private protected three methods of inheritance:

When public inherits the most common inheritance mechanism, the child class has the greatest permissions when it inherits, and the property is the same as the members of the base class and the protected member as the member of the derived class, while the private members of the base class remain private.

Protected is a kind of inheritance mechanism with more characteristics. The feature of protection inheritance is that all public and protected members of the base class become protected members of the derived class and can only be accessed by its derived class member functions or friend functions, and the private members of the base class are still private.

Private is a relatively uncommon method of inheritance, which is characterized by the public and protected members of the base class as private members of the derived class and cannot be accessed by subclasses of the derived class.

When a single inheritance is constructed, the first is to construct the base class, and then the process itself constructs. If the base class still exists in the upper base class, the construction of the upper base class is performed first, and then the base class is constructed, and the order of the destruction and the order of construction are strictly in reverse sequence of construction order.

Do not redefine inherited non-virtual functions to prevent the function from overwriting the phenomenon.

When the virtual function is implemented with default parameters, virtual functions are dynamically bound and the default parameters are statically bound. Default arguments declared in a base class virtual function, even if default parameters are re-specified in the derived class, the default parameters in the base class virtual function are still used in polymorphic calls

Do not attempt to overload virtual functions.


The difference between a value call, a reference value, and a pointer-to-value three ways:

1, the value of the transfer process, the parameters of the function as a local variable function, even if the stack of memory space in order to store the value of the real parameter passed in by the main function, thus becoming a copy of the argument, the value of the transfer is the function of the formal parameters of any operation as a local variable, It does not affect the value of the argument variable of the keynote function, which is undesirable if you want to achieve the exchange of two data by means of a value method.

2, in the reference transfer process, the parameters of the function of the parameter, although also as a local variable in the stack to open up memory space, but this is stored by the main function is put in the address of the argument variable. Any operation of the called function on the formal parameter is handled as a pointer indirection, that is, the argument variable in the keynote function is accessed through the address stored in the stack. Because of this, any manipulation of the modulated function on the parameter affects the argument variables in the keynote function.

3. The pointer passes the special case of the simultaneous value call, that is, the value of the pass is the address of the keynote function variable. The formal parameters of the modulated function also open up the inner space for local variables in the stack, and any operation of the modulated function on the local variable will function on the address of the key function variable. Therefore, any operation done by the modulated function through the local parameters will affect the argument variables in the keynote function.

The difference between a value call, a reference value, and a pointer-to-value three ways:

1, the value of the transfer process, the parameters of the function as a local variable function, even if the stack of memory space in order to store the value of the real parameter passed in by the main function, thus becoming a copy of the argument, the value of the transfer is the function of the formal parameters of any operation as a local variable, It does not affect the value of the argument variable of the keynote function, which is undesirable if you want to achieve the exchange of two data by means of a value method.

2, in the reference transfer process, the parameters of the function of the parameter, although also as a local variable in the stack to open up memory space, but this is stored by the main function is put in the address of the argument variable. Any operation of the called function on the formal parameter is handled as a pointer indirection, that is, the argument variable in the keynote function is accessed through the address stored in the stack. Because of this, any manipulation of the modulated function on the parameter affects the argument variables in the keynote function.

3. The pointer passes the special case of the simultaneous value call, that is, the value of the pass is the address of the keynote function variable. The formal parameters of the modulated function also open up the inner space for local variables in the stack, and any operation of the modulated function on the local variable will function on the address of the key function variable. Therefore, any operation done by the modulated function through the local parameters will affect the argument variables in the keynote function.


C + + experience clauses

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.