Several difficulties in C + + constructors (based on C + + 11)

Source: Internet
Author: User

Nearly one months have not updated the blog, it is time to move a pen! Because recently in the study of C + +, in the process of learning a lot of books, but also in the actual training encountered some problems. So in the next time, should be in C + + in some of the difficulties of their own to write a few topics, when it is for their own comb to consolidate knowledge!

We all know that a constructor is a class used to initialize individual data members (non-static), and if the members are not initialized well, then in the subsequent operations of the class will certainly encounter a variety of inexplicable errors. Therefore, it is necessary for us to have a comprehensive understanding of the constructors, to know how the constructors are executed in various situations, so that the class can be perfectly controlled.

Default constructor:

Class Base{public:    base () =default;    Base (string& s1,int A1): s (S1), a (A1) {}    base (base& B): s (B.S), a (B.A) {}    void print () {cout<<s; Cout<<a;} Private:    const string S;    int a=0;};

First, we all know that constructors are overloaded, and that objects of one class can support multiple initialization methods. So what if there are no constructors defined in the class? It doesn't matter, because the system automatically initializes individual data members within the class. For example, there is a string type of variable s in the class. The string's constructor is automatically invoked, the S is initialized to an empty string, and similar operations are performed for other data members. It is important to note that in C + + 11 A data member that supports a class is assigned a value in the declaration, so when the object of the base class is constructed by default, the value of member A is 0. So, what if there are constructors in the class? This time the system does not provide a default constructor, this time when you create the object of the class, and do not provide parameter initialization, the compiler will error. Of course, if you want to use the system default constructor is also possible. As shown in the code above, C + + 11 is as long as you add =default after the parameterless constructor name.

Initialization list:

Remember when you did not seriously learn C + +, always wonder what this initialization list is all about? is the assignment in the function body not the same? In fact, of course, it's not the same. In fact, before the function body of the constructor function, each data member is already initialized. If there is an assignment statement in the body of the function, yes, that is the assignment, not the initialization! This is especially noticeable when there is a const variable. Because a const variable can only be initialized and cannot be assigned a value. Just like s in the code above, when S is assigned in the function body, the system will error. This is the meaning of the initialization list.

Constructors for single-inheritance classes:

Class Derived:public Base{public:    derived () =default;    Derived (derived& der1): Base (Der1), B (der1.b) {}private:    int b=0;};
In the preceding code, the derived class is the inheriting class of the base class. There is a very important principle in the inheritance of classes, that is to do their own thing. For example, in the process of initializing the inheriting class, inheriting the part of the parent class of the class, it does not matter, just pass the argument to the parent class constructor, let it initialize itself well, it is to initialize its own new additions. So the question is, in the assignment constructor of the inheriting class, there is no problem with the assignment of the inheriting class part, but what about the base class part? Because the assignment constructor of a base class requires an object of the base class, the parameter is an inherited class. If you want to assign a value to a member of a base class, it is unrealistic, because inheriting class objects cannot access private members of the base class part. What about that? It is simple to assign the inherited class object as a parameter to the assignment constructor of the base class. Although the parameters required by the assignment constructor of the base class are references to the base class object, it is clear that That inherits the class object that contains the base class.。 Therefore, the system automatically initializes the base class part as long as the inherited class object is passed in the past. For constructors that inherit classes, it is also important to note the order in which they are initialized. We can think of the inheritance of the whole class as a tree, so the inheriting class can be seen as a leaf node. This seems to be very simple, the initialization process for inheriting classes is the process of gradually initializing from tree roots to leaves. As the code above, when initializing an object of the derived class, initialize its base section before initializing its own newly added part.

Constructors for multiple inheritance classes:

A multi-inheritance class constructor works in a way that is similar to a single-inheritance class. However, it is important to note that when there is a virtual base class in a multi-inheritance class, it is necessary to prevent the virtual base class from being repeatedly constructed . We are going to initialize it in the constructor of the current class. For example, we have the following inheritance relationship: A->b1->c,a->b2->c. Where a is the virtual base class of C. If we were to initialize in a single-inheritance way, obviously, because there are two paths, a is initialized two times. So when we construct C, we first initialize the virtual base class and C's constructor should use the following format:

C (...): A (...), B1 (..), B2 (...) ... { }

Even if the constructor of a is not explicitly declared, the system will still initialize a by default. And since the multiple class inherits from left to right initialization order, these four classes will be initialized in the order of: A,b1,b2,c.

These are some of my summary of the difficulties that exist in C + + constructors.





Several difficulties in C + + constructors (based on C + + 11)

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.