How does the C++11 delegate constructor attribute work?

Source: Internet
Author: User

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/71/41/wKiom1XJsHeDR-VjAADFDpJTzbo168.jpg "title=" 20120523_105622_1.jpg "alt=" Wkiom1xjshedr-vjaadfdpjtzbo168.jpg "/>

in code development, Both C and C + + are basic languages and are the entry-level must-learn languages for many Web developers . However, in c++98 , there are some problems in the class member construction problem, and some new features are proposed for this c++11.

C++98 If a class has more than one constructor and is implementing a class member construct, these constructors typically contain the same class member construction code as the basic. In the worst case scenario, the same class member construction statements are copied and pasted in each constructor.

based on class member constructs in c++98 problem, C++11 new features, programmers can set the public class member construction code in a constructor, which is called the target constructor. Other constructors implement class member constructs by calling the target constructor, which is called the delegate constructor. The constructor cannot be called explicitly until the new feature is presented, and the delegate constructor breaks this limitation. The following small series will be with you to see the C++11 delegate constructor usage.

1 , delegating the use of constructors

to learn more about C++11 new features, we can use the following example to see how it is used:

Program Source Code (delegatingctor1.cpp)

Class a{

Public

A (int i) is the delegate constructor for a ()

A (): a (0) {}

A (int i, INTJ) is a delegate constructor for a (int i)

A (int i): A (i,0) {}

Delegating the construction chain to a ()->a (int i)->a (int i, int j)

A (int i, int j) {

Num1=i;

Num2=j;

Average= (NUM1+NUM2)/2;

}

Private

int num1;

int num2;

int average;

};

As you can see from the above example, in the constructorA ()initialization list, the program calls theA (0),This is the syntax for delegating constructors. We callA(int i)to beA ()the target constructor, andA ()to beA (int i)the delegate constructor. Similarly,A (int i, int j)to beA (int i)the target constructor, andA (int i)to beA (int i, int j)the delegate constructor. After using the delegate constructor, the entire program becomes clearer and more concise. The target constructor and the delegate constructor have the same interface and syntax as other ordinary constructors, and they do not have special processing and labeling. By this example, we can also seeC++11a delegate constructor can be a target constructor for another delegate constructor, and the delegate constructor and the target constructor are relative. The target constructor is selected by overloading and class argument derivation criteria.

in the During C + + function delegation, when the target constructor function finishes executing, the delegate constructor continues to execute other statements within its own function. This can be done in the following example:

Program Source Code (delegatingctor2.cpp)

#include <iostream>

Using NAMESPACESTD;

Class a{

Public

A (): a (0) {cout << "in A ()" << Endl;}

A (int i): A (i,0) {cout << "in A (int i)" << Endl;}

A (int i, int j) {

Num1=i;

Num2=j;

Average= (NUM1+NUM2)/2;

cout << "in A (int i, int j)" << Endl;

}

Private

int num1;

int num2;

int average;

};

int main () {

A;

return 0;

}

The output of this example is:

In A (int i, INTJ)

In A (int i)

In A ()

2 , delegating exception handling for constructors

When the target constructor throws an exception, the exception is fetched by the try module in the delegate constructor . And in this case, the code in the delegate constructor's own body will not be executed.

Let's take a look at the example below, the constructor A (int i, int j) throws an exception, which in turn is fetched by the delegate constructor a (int i) and A () , and the function body of a (int i) and A () is not executed.

Program Source Code (exception1.cpp)

#include <iostream>

Using NAMESPACESTD;

Class a{

Public

A ();

A (int i);

A (int i, int j);

Private

int num1;

int num2;

int average;

};

A:: A () try:a (0) {

A () function body is not executed to

cout << "A () body" << Endl;

}

catch (...) {

cout << "A () Catch" << Endl;

}

a::a (int i) try:a (i, 0) {

A (int i) function body is not executed to

cout << "A (int i) body" << Endl;

}

catch (...) {

cout << "A (int i) catch" << Endl;

}

a::a (int i, INTJ) try {

Num1=i;

Num2=j;

Average= (NUM1+NUM2)/2;

cout << "A (int i, int j) Body" << Endl;

Throw Exception

Throw 1;

}

catch (...) {

cout << "A (int i, int j) Catch" << Endl;

}

int main () {

try{

A;

cout << "main body" << Endl;

}

catch (...) {

cout << "Main catch" << Endl;

}

return 0;

}

The output of this example is:

A (int i, int j) Body

A (int i, int j) Catch

A (int i) catch

A () Catch

Main catch

When the delegate constructor throws an exception, the system automatically calls the destructor for the object within the target constructor that has already been constructed. In the following example, the target constructor a (int i, int j) completes the construction of Object A. Its delegate constructor a (inti) throws an exception at execution time , and object A is immediately refactored, and the function body of the delegate constructor A () of a (int i) is no longer executed by the compiler. This is consistent with the principles described in the previous example.

Program Source Code (exception2.cpp)

#include <iostream>

Using NAMESPACESTD;

Class a{

Public

A ();

A (int i);

A (int i, int j);

~a ();

Private

int num1;

int num2;

int average;

};

A::a (): A (0) {

A () function body is not executed to

cout << "A () body" << Endl;

}

a::a (int i) try:a (i, 0) {

cout << "A (int i) body" << Endl;

throws an exception, object a will be destructor

Throw 1;

}

catch (...) {

cout << "A (int i) catch" << Endl;

}

a::a (int i, INTJ) {

Num1=i;

Num2=j;

Average= (NUM1+NUM2)/2;

cout << "A (int i, int j) Body" << Endl;

}

A::~a () {

cout << "~a () body" << Endl;

}

int main () {

A;

return 0;

}

The output of this example is:

A (int i, int j) Body

A (int i) body

~a () body

A (int i) catch

3 , delegating constructors, and generic programming

in fact, delegating constructors can make it easier to program generic programming of constructors in addition to allowing programmers to circumvent repetitive code in constructors, such as the following example:

Program Source Code (generic.cpp)

#include <iostream>

Using NAMESPACESTD;

Template<typenamet> class a{

Public

A (int i): A (i,0) {}

A (double D): A (d, 0.0) {}

function Templates

A (t I, T j) {

Num1=i;

Num2=j;

Average= (NUM1+NUM2)/2;

cout << "average=" << average << Endl;

}

Private

T NUM1;

T num2;

T average;

};

int main () {

A<int>a_int (1);

A<double>a_double (1.0);

}

The output of this example is:

Average=0

average=0.5

in the above code, the target constructor is a function template, which is instantiated when the delegate constructor is called, so it is very convenient to write the same type of target constructor without the developer.

Summarize

through the above example, I believe that everyone C++11 Delegate constructors have a certain understanding and understanding of the specific use. Using This feature of c++11 can improve the readability and maintainability of the program, and improve the development efficiency of developers. Although it may take some overhead to delegate a call to a constructor, you can still try it.


How does the C++11 delegate constructor attribute work?

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.