C ++ learning experience - constant, initialization

xiaoxiao2021-03-18  211

1. Write a new class When you pay attention to the default replication constructor and the behavior of assignment operations meet the expected requirements. Generally, those classes containing pointers should pay special attention. If the object being directed is to be the resulting object, then the default replication constructor may be erroneous because it is just a simple copy of the pointer rather than the pointer. Object. If you have a copy constructor for class, it is very difficult to declare it to private and do not provide any definitions, prevent unintentional calls from decrease due to errors generated by the default replication constructor.

2, (operator =) Assignment operator should return a constant reference to an assignment object, such as Const className & ClassName :: className () {// Specific Detail} Prevents the assignment results as left values, such as: (a = B) = C;

3, static members in the class must initialize (outside the class) format: type classname :: variantname =?

4. References must be initialized when defined, and the reference is initialized, it is maintained on a certain target, and no longer separate. Any assignment to this reference is assigned to the target of the reference, rather than the reference to the other target. As the result of the following procedure is 33 44, after the value of B is given, the value of A is also changing. #include int main () {Int a = 3, b = 4; int & c = a; cout << c << a << endl; c = b; cout << c << a << ENDL; RETURN 0;

5. By default, all members of the object have been initialized by their default constructor before the function of constructors is executed. Those who do not construct a function will have an undefined initial value. If we add a colon and a list of initial bodies after the end of the parentheses ending in the constructor definition, each initial body includes a name and a list of parameters, which will tell the compiler to initialize the list only in the list. the member of. If there is such a constructor: classname :: classname (const string & variantname) {variant = variantname;} At this time, since string is a class, class classname member Variant will first initialize the default constructor of String, its value is not , Then in the constructor of the classname, its value will change again because of assignment operation. This is two steps that can be synthesized by the above method, such as: ClassName :: ClassName (const string & variantname): Variant (variantname) {}

6, the initialization of constants and references must be placed when the constructor is building a data member structure, that is, behind the colon of the constructor. The members in the class are constructed in the order declared in the class, rather than seeing the order of the members initialized after the constructor. The reason is that if a special constructor is specified in the constructor, the destructor has to query the definition of the constructor to obtain the order of the designer, due to the constructor and the destructor can be in different files. In definition, this will cause a challenge for the implementation of the compiler. Moreover, a class can have two or more constructor, and we do not guarantee the members in these constructors. The implementation is consistent. So use the declaration of the class to solve the constructor's constructor and sequence. 7, Thing get_A_THING (); void change_thing (thing "; chang_thing (GET_A_THING ()); // Compiling an error to pass a unnamed temporary object to a very quantitative pointer to function is illegal, if the function is adjusted to reference parameters The value is modified, but the call function ignores this modification and thinking that this behavior is a bug. This is ok: void Look_at_thing (const thing "; Void Look_at_thing (GET_A_THING ()); 8. Adding a keyword const with the parameter list of the function of the function, we can declare a member function as a constant Member functions, but we can only call constant members functions on constant objects. In a constant member function definition body, all data members of the object are constants, the "THIS" pointer is also a "constant pointer to the constant object" instead of "pointing the object's constant pointer". Therefore, constant objects cannot call a very amount of member functions but a very number of objects can not only call a very square member function, but also call constant member functions.

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

New Post(0)