Experience notes for constructor in C ++

xiaoxiao2021-03-05  25

Today, once again drilled the C tutorial, self-feeling, some of the constructor of the class, have new comprehension and understanding, write it here, I hope to share with friends, I hope we will progress together:

1. If the new class established, the user does not provide any constructor, the system provides the default constructor, and the initialization of each member variable in the class is initialized. If the user provides any constructor, the default constructor provided by the system will no longer be effective. . . . At this point, if you want to use the default constructor without parameters, you must handle the default constructor.

2, in the function, all variables and objects are in the order in which they are defined, and the unified definition configuration is unified when the function starts.

Such as follows:

void mian ()

{

INT m = 2;

IF (m == 2) goto ABC;

Int n;

ABC: n = 3; // The N here has been defined when the function starts execution.

}

3. Global variables, that is, before entering the main function, that is, it has been constructed, and the structure of these global variables is in order. . . . . So, don't rush to allow a global variable to access another global variable. . . .

4. For the nesting of the class, the initialization of the class member is required if the non-default constructor (ie, the constructor with parameters with parameters) is called in the constructor. Use: (colon) initialization method. In addition, the constant member variables, the member function of the reference type must also initialize after the colon (initialization member mode of constructor).

5. I think the mode of operation and order of the constructor should be like this:

First, if the class is a derived class, the base class constructor is first called, the base class is constructed, and then the initialization method behind the class member variable is called (such as the constructor of the member object, etc.) for members. The structure of the variable object, if the colon is not shown to indicate the constructor of the member variable, call the default constructor to initialize, and then enter the constructor, complete the entire constructor. "This is my understanding, I don't know if it is right. . . "

6, Question: Hope to see the master to answer:

In the derived class, the operator "=", how to use the "=" function body of the base class. . . It is said that "=" "=" in some members in the base class is to achieve, and will not be column one by one. ? ? ?

In addition, if Which master sees the mistake in the article, I hope to give me a message.

My QQ: 7754350, E_MAIL: LUMINGY9@tom.com

转载请注明原文地址:https://www.9cbs.com/read-32153.html

New Post(0)