为什么C ++ 11引入了委托构造函数? [英] Why did C++11 introduce delegating constructors?

查看:173
本文介绍了为什么C ++ 11引入了委托构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白委派构造函数的用处。简而言之,如果没有委托的构造函数是无法实现的?

I cannot understand what the use is of delegating constructors. Simply, what cannot be achieve without having delegating constructors?

它可以做一些简单的事情

It can do something simple like this

class M 
{
 int x, y;
 char *p;
public:
 M(int v) : x(v), y(0), p(new char [MAX]) {}
 M(): M(0) {cout<<"delegating ctor"<<endl;}
};

但是我不认为有必要为这种简单的功能引入新功能吗?可能是我无法理解重点。知道吗?

But I don't see it is worth introduce a new feature for such a simple thing? May be I couldn't recognize the important point. Any idea?

推荐答案

委派构造函数可以防止代码重复(以及随之而来的所有可能的错误和缺陷:增加维护,减少可读性...),这是一件好事。

Delegating constructors prevent code duplication (and all the possible errors and flaws that come with it : increased maintenance, decreased readability...), which is a good thing.

这也是委派初始化列表(用于成员和基础初始化)的唯一方法,即,您真的不能通过共享<$ c来替换此功能$ c> Init()方法供您的构造函数使用。

It is also the only way to delegate the initialization list (for members and bases initializations), i.e. you really can't replace this feature by having a shared Init() method for your constructors.

示例:

1)来自 N1986提案

class X { 
 X( int, W& ); 
 Y y_; 
 Z z_; 
public: 
 X(); 
 X( int ); 
 X( W& ); 
}; 
X::X( int i, W& e ) : y_(i), z_(e) { /*Common Init*/ } 
X::X() : X( 42, 3.14 )             { SomePostInitialization(); } 
X::X( int i ) : X( i, 3.14 )       { OtherPostInitialization(); } 
X::X( W& w ) : X( 53, w )          { /* no post-init */ } 

2)具有构造函数和复制构造函数的委托,也来自N1986提案

2) Delegation with both constructor and copy constructor, also from N1986 proposal :

class FullName { 
 string firstName_; 
 string middleName_; 
 string lastName_; 

public: 
 FullName(string firstName, string middleName, string lastName); 
 FullName(string firstName, string lastName); 
 FullName(const FullName& name); 
}; 
FullName::FullName(string firstName, string middleName, string lastName) 
 : firstName_(firstName), middleName_(middleName), lastName_(lastName) 
{ 
 // ... 
} 
// delegating copy constructor 
FullName::FullName(const FullName& name) 
 : FullName(name.firstName_, name.middleName_, name.lastName_) 
{ 
 // ... 
} 
// delegating constructor 
FullName::FullName(string firstName, string lastName) 
 : FullName(firstName, "", lastName) 
{ 
 // ... 
} 

3) MSDN给出了此示例 ,由构造函数执行参数验证(如前所述,这种设计值得商)):

3) MSDN gives this example, with constructors performing argument validation (as commented, this design is debatable) :

class class_c {
public:
    int max;
    int min;
    int middle;

    class_c() {}
    class_c(int my_max) { 
        max = my_max > 0 ? my_max : 10; 
    }
    class_c(int my_max, int my_min) { 
        max = my_max > 0 ? my_max : 10;
        min = my_min > 0 && my_min < max ? my_min : 1;
    }
    class_c(int my_max, int my_min, int my_middle) {
        max = my_max > 0 ? my_max : 10;
        min = my_min > 0 && my_min < max ? my_min : 1;
        middle = my_middle < max && my_middle > min ? my_middle : 5;
    }
};

感谢构造函数委派,它减少为:

Thanks to constructors delegation, it reduces to :

class class_c {
public:
    int max;
    int min;
    int middle;

    class_c(int my_max) { 
        max = my_max > 0 ? my_max : 10; 
    }
    class_c(int my_max, int my_min) : class_c(my_max) { 
        min = my_min > 0 && my_min < max ? my_min : 1;
    }
    class_c(int my_max, int my_min, int my_middle) : class_c (my_max, my_min){
        middle = my_middle < max && my_middle > min ? my_middle : 5;
}
};






链接:

  • Delegating Constructors (r3) Proposal - N1986
  • Stroustrup C++ FAQ : Delegating constructors

这篇关于为什么C ++ 11引入了委托构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆