C + + Primer notes-Class

Source: Internet
Author: User
Tags class definition

1. A function defined inside a class is an implicit inline function.

2. Because the purpose of this is always pointing to the "this" object, this is a constant pointer and we are not allowed to change the address saved in this.

3. Constant member function: Allows the const keyword to be placed after the parameter list of the member function, and the const that immediately follows the argument list indicates that this is a pointer to a constant. Because this is a pointer to a constant, the constant member function cannot change the contents of the object that invokes it.

4. Constant objects, as well as references or pointers to constant objects, can only call constant member functions.

5. The compiler processes the class in two steps: the declaration of the member is compiled first and then the member function body. The member function body is therefore free to use other members of the class without caring about the order in which the members appear.

6. Constructors cannot be declared as const, and when we create a const object of a class, the object can actually get its "constant" property until the constructor finishes the initialization process. Therefore, a constructor can write a value to a const object during its construction.

7. The compiler automatically generates a default constructor when the class does not declare any constructors, and the default constructor initializes the data members of the class according to the following rules:

If there is an initial value within the class, it is used to initialize the member.

Otherwise, the member is initialized by default.

8. We can use default to require the compiler to produce constructors.

class test{public:    default;    Test (int  i) {}};


9. The only difference between defining classes using class and struct is the default access permissions, and class defaults to public by default, Private,struct.

10. The class allows other classes or functions to access its non-public members by another class or function as its friend, and the friend declaration can only appear inside the class definition and is not restricted by the access control.

class test{    int gettestcount (const test& t);  Public :     default ; Private :     int M_count;}; int Gettestcount (const test& t) {    return  t.m_count;}

11. Although there is no need to change user code when the definition of a class changes, the source files that use the class must be recompiled.

12. A mutable data member is never const, even if it is a member of a const object. Thus a const member function can change the value of a mutable member.

class test{public:    default;     void Const {m_count++;} Private :     int M_count;};


13. A const member function if the *this is returned as a reference, its return type will be a constant reference.

class test{public:    default;     Const Const return *this;};


14. Each class is responsible for controlling its own friend or friend function.

class test{    class best ; Private :     int M_count;}; class best{public:    intreturn  m_t.m_count;} Private :    test m_t;};

classtest;classbest{ Public:    intGet_test_count (Consttest&t);};classtest{friendintBest::get_test_count (Consttest&t);Private:    intM_count;};//Note that this definition must be after the test definition, otherwise you will not know the details in the test class would be errorintBest::get_test_count (Consttest&t) {    returnT.m_count;}

15. Unlike other local scopes, type names that have already been defined in the outer scope cannot be repeatedly defined in the class, even if the definitions are the same.

int My_int; class test{private:    int my_int;        // errors, repeated definitions, but some compilers can compile     my_int M_count;};

16. The order in which the name is used in the member function is: member function, class-I scope.

int count; class test{private:    int  count;     int add_count (int  count) {        this->count + = count;    // This->count refers to a member of a class, and count refers to a parameter        :: count++;                // :: Count refers to the outer scope of Count     }};


17. If the member is not initialized in the constructor's initialization list, the default initialization is performed before the body of the constructor.

18. If the member of a class is a const or a reference or a class type that does not have a default constructor, initialization must be done either at the time of definition or in the initialization list.

class test{public:    Test () {};     // error, M_const not initialized Private :     Const int M_count;};


19. The order in which the members appear in the constructor's initialization list does not represent the order of initialization, but is initialized in the order in which they are defined in the class. So if a member is initialized by another member, be careful.

class test{public:    Test ()        : m_base (0)            //  m_base value is 0        , M_count (m_base)    // the value of the m_count is undefined     {}; Private :     int M_count;     int m_base;};


20. If a constructor defines a default argument for all parameters, the class actually defines the default constructor.

21. A delegate constructor refers to the process by which a constructor calls other constructors to perform its own initialization.

class test{public:    Test (int i,int j) {}        //  non-delegate constructor     Test (int0) {}    //  delegate constructor, delegate test (Int,int) constructor    Test (int i): Test () {}        //  delegate constructor, delegate default constructor };


22. The default constructor is executed when all variables are not fully initialized in an existing constructor.

 class   test{ public   int   i) {}  private  :  int   M_count;};  struct   a{test T;};        A;  //  error, cannot be a composite default constructor  struct   b{B () {}  //  error, T has no initial value   Test t;};  
class test{public:    Test (int  i) {}};test T ();     // correctly, T is a function of the declared test T1;    // error, test has no default constructor


23. If a constructor accepts an argument, it actually defines a conversion constructor that is converted to such a type, but the conversion allows only one-step class-type conversions.

class test{public:    Test (std::string  str) {}}; void get_test (Test t) {}get_test ("123");                // error, "123" is neither a test type nor a string type get_test (std::string("123" ));    // correct, String type is converted to test type

But in fact, after the completion of the conversion we do not use it, if you do not want this conversion can be used explicit

class test{public:    explicit Test (std::string  str) {}}; void get_test (Test t) {}get_test (std::string("123"));    // error, this implicit conversion is not allowed


24. The aggregation class must meet the following conditions:

    • All members are public
    • No constructors are defined
    • No intra-class initial values
    • There is no base class, and there is no virtual function
struct test{    std::string  name;     int  "test",1 };


25. A data member is a literal type of aggregation class that is a literal constant class, or a literal constant class that meets the following criteria:

    • Data types must be constant types
    • A class must contain at least one constexpr constructor
    • If a data member contains an initial value within the class, the initial value of the built-in type member must be a constant expression, or if the member belongs to a kind of type, the initial value must use the member's own constexpr constructor
    • Class must use the default definition of a destructor that is responsible for destroying the object of the class

Because the constructor does not return a statement, and the only executable statement of the CONSTEXPR function is the return statement, you can infer that the constexpr constructor is actually an empty statement.

The constexpr constructor must initialize all data members, either using the constexpr constructor or a constant expression.

26. A static member of a class exists outside any object, and the middle of the object contains any data related to the static data member. Similarly, a static member function is not bound to any object, nor does it contain the this pointer. As a result, static member functions cannot be declared as Const.

27. In general, we cannot initialize static members within a class, when static members are defined outside the class, the static keyword cannot be duplicated, and once defined, it will persist throughout the lifetime of the program.

28. The difference between a static member and an ordinary member:

class test{    Test (int i = m_count);        // static members can be default arguments, and ordinary members can not Private :     Static int M_count;                  static test m_t;            // correct, a static member can be an incomplete type    test* m_t1;                    // correctly, the pointer can be an incomplete type    Test m_t2;                    // error, the data member must be of the full type };

C + + Primer notes-Class

Related Article

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.