C ++ learning experience-constant, initialization

Source: Internet
Author: User

1. When writing a new class, pay attention to whether the default copying constructor and assignment operations meet the expected requirements. Generally, special attention should be paid to the classes that contain pointers. If the object to be directed belongs to the generated object, the default copy constructor may be wrong, because it simply copies the pointer rather than the object pointed to by the pointer. If it is very difficult to implement a replication constructor for a class, it is declared as private and does not provide any definition. This prevents unintentional calls to reduce errors caused by executing the default replication constructor.

2. (operator =) the value assignment operator should return a constant reference to the assigned object, for example, const classname & classname: classname () {// specific implementation details} block the use of the value assignment result as the left value, for example: (A = B) = C;

3. Static members in the class must be initialized (outside the class) in the format of: Type classname: variantname = ?;

4. The reference must be initialized during definition. Once the reference is initialized, it will be maintained on a certain target and will not be separated. Any value assigned to this reference is a value assigned to the target maintained by the reference, rather than holding the reference to another target. See the followingProgramThe result is 33 44. After the value of B is given to C, the value of a also changes.
# Include <iostream. h>
Int main ()
{
Int A = 3, B = 4;
Int & C =;
Cout <C <A <Endl;
C = B;
Cout <C <A <Endl;
Return 0;
}

5. By default, before the constructor is executed, all the members of the object are initialized by their default constructor. Members without constructors will have an undefined initial value. If we add a colon and an initial body list after the brackets ending with the parameter list in the constructor definition, each initial body includes a name and a parameter list, the compiler will be told to use the except function call only to initialize the members in the list. If there is such a constructor:
classname: classname (const string & variantname)
{< br> variant = variantname;
}< br> at this time, because string is a class, the classname member variant will be initialized by the default constructor of string, and its value is not fixed, then in the classname constructor, its value will be changed again because of the value assignment operation. This is two steps. You can use the above method to synthesize one step, for example:
classname: classname (const string & variantname): variant (variantname) {}

6. constants and references must be initialized when the constructor is establishing a data member structure, that is, placed after the constructor's colon. The members in the class are constructed in the order they are declared in the class, rather than the initialization sequence of the members after the colon in the constructor description. The reason is that if a special construction order is specified in the constructor, the constructor has to query the definition of the constructor to obtain the order in which the members are parsed, since constructor and destructor can be defined in different files, this will cause a difficult problem to the implementer of the compiler, A class can have two or more constructors. We cannot guarantee that the implementations of these constructors are consistent. So the class declaration is used to solve the structure and structure sequence of members.
7. Thing get_a_thing ();
Void change_thing (thing &);
Chang_thing (get_a_thing (); // compilation Error
It is invalid to pass an unnamed temporary object to the function as a very large pointer. If the called function modifies the value of the referenced parameter, but the called function ignores this modification, this behavior is considered a bug. This is acceptable:
Void look_at_thing (const thing &);
Void look_at_thing (get_a_thing ());

8. By adding the keyword const after the parameter list of the Declaration body and definition body of the function, we can declare a member function as a constant member function, however, we can only call a constant member function for a constant object. In the constant member function definition body, all data members of an object are constants. The "this" pointer is also a "constant pointer to a constant object" instead of "constant pointer to an object ". Therefore, a constant object cannot call a very large number of member functions, but a very large object can call not only a very large number of member functions, but also a constant member 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.