C + + supplements--constructors

Source: Internet
Author: User

C + + supplements--constructors

Objective

For a class, the constructor is probably the most important member of the function. There are a variety of details about constructors, and as new standards are presented, constructors have new features. This article focuses on those little-known aspects of the next constructor.

constructor function

The role of constructors is well known: When objects of a class are created, control the initialization and assignment of objects.

General form of the constructor:

class name (arg_list);

Where Arg_list is a comma-separated list of parameters.

Features: No return value type, and no const limit.

Default constructor

It should be noted that the parameterless constructor is the default, and the constructor with the default arguments is the default construct.

Examples show that:

#include <iostream>using namespace Std;class myclass{protected:int a = 0;int b{1};p ublic:myclass (int a = 0, int b = 0): A (a), B (b)   //with default arguments constructed by default constructs {cout << "MyClass (int a = 0, int b = 0)" << Endl;} int Geta () Const{return A;} int Getb () Const{return b;}; int main () {MyClass my;   Call the default construct cout << "a =" << My.geta () << ends << "b =" << My.getb () << endl;cin.get (); r Eturn 0;}
Run



From the running result, you can verify the conclusion. A constructor with a parameter that has a default argument is also the default construct, which is probably something that many people can easily ignore.

The default constructs a rule that initializes the data members of the class:

    1. If there is an initial value within the class, it is used to initialize the member.
    2. Otherwise, the default is initialized. (although this rule is standard, most compilers do not)

Several concepts1. The initial value in the class. In the example code, int a = 0; int b{1}; This form is the initial value in the class. The following describes when to use the initial value in the class.
2. Default initialization. built-in types, such as int,double types. int->0,double->0.0. is the default initialization. The class type, initialized with the default construction of the class, is the default initialization.
The default construct does not always exist, and if another constructor (non-default construct) exists, the compiler does not provide a default construct without parameters. The new standard proposes a way to: such as MyClass () = default; This use of the keyword default indicates that the compiler is required to generate a default constructor.
3. List of initial valuesconstructor MyClass (int a = 0, int b = 0): A (a), B (b) {...} where A (a), B (b) is the list of initial values.
4. What is initialization? What is an assignment? objects are created in memory (with a memory entity), and the first value is called initialization. Overwrite the original value, called the assignment.
In General, the constructor completes the initialization in the list of initial values and completes the assignment in the body of the function. This conclusion is not a good proof, but there are ways to do so, by means of: Const type and reference type. The following code:
#include <iostream>using namespace Std;class myclass{protected:int a = 0;const int ca = 0;int& Ra;public:myclass (int a): A (a), CA (a), RA (this->a) {}int geta () Const{return A; void Printca () const{cout << "CA =" << ca << Endl;} void Printra () const{cout << "RA =" << ra << Endl;}; int main () {MyClass my (1); cout << My.geta () << endl;my.printca (); My.printra (); Cin.get (); return 0;}
Run

Const types and reference types, which must be initialized when they are created. If the CA = A; RA = this->a; Moved inside the constructor body, it cannot be compiled. In other words, once the constructor body is entered, the initialization is complete.




Directory of all content
    • CCPP Blog Directory



C + + supplements--constructors

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.