How to use the C++11 delegate constructor

Source: Internet
Author: User
Tags throw exception

In code development, bothC 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 the problem of class member construction in C++98 , c++11 the new feature, the programmer can set the common 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 the new features of c++11, we can use the following example to see how this is done:

Program source code (DELEGATINGCTOR1.CPP)

Class a{

Public

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

A (): a (0) {}

A (int i, int j) 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 initialization list of constructor a (), the program calls a (0), which is the syntax for delegating constructors. We call a (int i) the target constructor for a (), and A () is the delegate constructor of a (int i). Similarly, a (int i, int j) is the target constructor for a (int i), and a (int i) is a delegate constructor for a (int i, int j). 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. With this example, we can also see that a delegate constructor in C++11 can be the target constructor for another delegate constructor, which is relative to the delegate constructor and the target constructor. The target constructor is selected by overloading and class argument derivation criteria.

During the C + + function delegation process, 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 namespace Std;

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, int j)

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, where constructor a (int i, int j) throws an exception, which is then 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 namespace Std;

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, int j) 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 (int i) 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, which is consistent with the principle described in the previous example.

Program source code (EXCEPTION2.CPP)

#include <iostream>

using namespace Std;

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, int j) {

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 namespace Std;

Template<typename T> 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 you have a certain understanding and understanding of the specific use of c++11 delegated constructors. 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.

Recommended Learning: "C + + object-oriented programming "

How to use the C++11 delegate constructor

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.