C + + 34th day: Some content related to classes

Source: Internet
Author: User

Review

The following content contains personal insights

Ⅰ, sometimes have to use the constructor initializer list, because some compilers do not support the initial values in the class, which prevents initialization within the function body.

It is important to note that data members that are ignored by the initialization list are initialized by default (the result depends on the location and can have dire consequences). Here are the N cases of the constructor:

    1. The programmer directly ignores the constructor--the constructor that produces the default composition--the member is initialized by default--an indeterminate result.
    2. Provide the initial value to the data member (so-called property initialization in Java), do not write the constructor--the default composition constructor uses the provided initial value to initialize the corresponding member--the ignored member is initialized by default.
    3. The initial value in the class is not used, and the constructor initializer list-the ignored member is initialized by default. 、
    4. ·····

The situation is so dizzy, it is better to remember the better practice: provide the initial value in the class + display to initialize each member without the initial value in the class.

This is summed up by ensuring that each member has a definite value after creation, but not repeated initialization (repeated initialization: the initial value in the class is the same as the constructor assignment).

Ⅱ, copy, assignment, and destructor are also available in synthetic versions!

The compiler will do it for us if it is not written, but in some cases we cannot rely on the synthesized version:

That is -tube-reason-dynamic-state-inner-stored Class!!! (manual emphasis, except for the class containing string and vector)

Methodological summary:

    1. There is usually no need to write manually.
    2. When appropriate, use string and vector to dynamically allocate memory as much as possible !

Ⅲ, friend functions require a special declaration outside of the class. Example:

Class.h

#ifndef class_h_included #define Class_h_includedclassint getno (const person&); Private :         int  - ;};  int getno (const//  necessary#endif

Class.cpp

" Class.h " int getno (const person& x) {    return  x.no;}

Main.cpp

" Class.h "  <stdio.h>int  Main () {person    p;    printf ("%d\n", Getno (p));     return 0 ;}

Operation Result:

[Email protected]:~/cprg# g++-std=c++ one main. CPP class. CPP [email protected]:~/cprg#./

Related reading:

    • C + + Step-up compilation
    • C + + multi-file organization
    • What is the relationship between the C + + header file and its implementation file CPP?

C + + 34th day: Some content related to classes

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.