C ++ experience and experience

Source: Internet
Author: User

C ++ experience and experience

C ++ experience:

I. constructor is never called a virtual function:

In the simplest way, the C ++ object model manages virtual functions based on the virtual function table. Therefore, when calling a virtual function, you need to find the virtual function table, when the object is not created successfully, there is no virtual function table pointer. The constructor constructs the object. It is unreasonable to search for the virtual function table before the object is created successfully.

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

A class displays the declared constructor. In this case, the compiler does not generate a public default constructor. If the program requires a default constructor, the programmer must provide it explicitly.

A class declares a non-Public constructor, And the compiler does not generate a Public default constructor.

If a class has inheritance, the base class is constructed first, and then the inherited class is constructed. In the structure, the order is the opposite.

If the base class has virtual functions, you must create a virtual function table in the construction of the inheritance class.

 

Call the copy constructor:

When one object of the class initializes another object of the class, the copy constructor is called.

If the parameter of a function is a class object, when you call a function to combine the parameter with the real parameter, the copy constructor is called.

If the return value of a function is a class object, the copy constructor is called when the function is returned after the function call is completed.

 

Avoid calling virtual functions in constructor/destructor:

The base class constructor is called before the execution of the derived class constructor. Therefore, when the base class constructor is run, no data member of the derived class is initialized.

The Calling mechanism of virtual functions is completely controlled by the base class. Therefore, if the base class is not constructed, the calling mechanism of virtual functions (both virtual function tables) is not initialized, all the expected operations implemented through virtual functions will fail.

 

What is the relationship between the Class object size:

If no data member exists in a class, the class size is 1.

The object size in Class is only related to the data member size of the object, but not to the function Member of the object.

The memory size occupied by non-static data members of the Class object will affect the object size.

The memory alignment policy adopted by the Class object will affect the Class object size.

The data member in the Class object does not affect the object size.

The virtual function in Class will affect the Class object size. Because of virtual-talbe, the virtual function occupies 4 bytes of space.

Virtual inheritance affects the object size:

Because it involves virtual function tables and virtual base tables, A vfptr pointer pointing to the virtual function table vftable and a vbptr pointer pointing to the virtual base table vbtable will be added at the same time, the space occupied by the two is 8 (or 8 multiplied by the number of parent classes for multi-inheritance). Remember to have their own virtual function table pointers and virtual base class pointers, all share the virtual base class pointer.

The size of the class object does not affect static data members because the tired static data members are distributed in the global storage area and do not occupy the space of the Class Object.

 

Operator overload:

Do not reload the & and | Operator, because the & and | overload causes & and | to lose the short evaluate function.

If an overload operator is a class member, it will be called only when the left operand used with it is the class object. If the left operand of this operator must be of another type, the overload operator must be a namespace member.

 

Public private protected:

The most common inheritance mechanism for Public inheritance. When public inheritance is used, the subclass has the maximum permission. The characteristics of Public inheritance are that the base class public members and the protected members are the members of the derived class, they both remain in the original state, while the private members of the base class are still private.

Protected is a special Inheritance Mechanism. The feature of protection inheritance is that all the public and protected members of the base class become the protected members of the derived class and can only be accessed by the member functions or friends functions of the derived class, the Private Members of the base class are still private.

Private is a rare inheritance method. Private inheritance features that the public and protected members of the base class are both Private members of the derived class and cannot be accessed by the subclass of the derived class.

 

During construction of a single inheritance, the base class is constructed first, and then the process itself is constructed. If the base class still has the upper-layer base class, first construct the upper-layer base class and then construct the base class, the structure order and construction order are strictly in the reverse order of the construction order.

 

Do not redefine the inherited non-virtual functions to prevent function coverage.

 

When virtual functions with default parameters are implemented, virtual functions are dynamically bound, and default parameters are statically bound. The default parameter declared in the base class virtual function. Even if the default parameter is re-specified in the derived class, the default parameter in the base class virtual function is still used in the multi-state call.

 

Do not try to reload the virtual function.


Differences and links between the following methods:

1. During value transfer, parameters in the form of the called function are processed as local variables of the called function, even if the memory space is opened in the stack to store the real parameter values passed by the main function, as a copy of the real parameter, the value transfer feature is that the called function performs any operation on the form parameter as a local variable without affecting the value of the real parameter variable of the main function, this method is not applicable if you want to exchange two data values by passing values.

2. During the reference transfer process, although the form parameter of the called function is also used as a local variable, the memory space is opened up in the stack, however, the address of the Real Variable put in by the main function is stored. Any operation of the called function on the form parameter is processed as indirect pointer addressing. That is, the address stored in the stack is used to access the real variable in the main function. Because of this, any operation by the called function on the form parameter affects the real parameter variables in the main function.

3. The special case of passing a value when passing a pointer, that is, the address of the passed value as the main function variable. Parameters in the form of the called function also open up internal space for local variables in the stack. Any operation of the called function on the local variables will function above the address of the main function variable. Therefore, any operations performed by the called function through local parameters will affect the real parameters in the main function.

Differences and links between the following methods:

1. During value transfer, parameters in the form of the called function are processed as local variables of the called function, even if the memory space is opened in the stack to store the real parameter values passed by the main function, as a copy of the real parameter, the value transfer feature is that the called function performs any operation on the form parameter as a local variable without affecting the value of the real parameter variable of the main function, this method is not applicable if you want to exchange two data values by passing values.

2. During the reference transfer process, although the form parameter of the called function is also used as a local variable, the memory space is opened up in the stack, however, the address of the Real Variable put in by the main function is stored. Any operation of the called function on the form parameter is processed as indirect pointer addressing. That is, the address stored in the stack is used to access the real variable in the main function. Because of this, any operation by the called function on the form parameter affects the real parameter variables in the main function.

3. The special case of passing a value when passing a pointer, that is, the address of the passed value as the main function variable. Parameters in the form of the called function also open up internal space for local variables in the stack. Any operation of the called function on the local variables will function above the address of the main function variable. Therefore, any operations performed by the called function through local parameters will affect the real parameters in the main 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.