C++11特性-委派构造函数-的益处

7 篇文章 0 订阅

对于复杂的C++类,有很多个构造函数是非常常见的。为了减少重复代码,经常看到在一个构造函数里调用另一个构造函数的情况。在看C++11新特性时,发现了一些与原有认知不同的内容。就做了下验证。

#include <iostream>
using namespace std;

class Info{
public:
    Info() { Init(); printInfo();};
    Info(int i) { Info(); type = i; printInfo();}
    Info(char e) { name = e; Info(); printInfo();}

private:
    void Init() {rest = 2;}
    void printInfo() {cout << this << ":" << "type : " << type << "; name : " << name << "; rest : " << rest << endl;}
    int type {1};
    char name {'a'};
    int rest {999};
};

int main()
{
    Info info;
    cout << "----------------" << endl;
    Info Info2(33);
    cout << "----------------" << endl;
    Info info3('e');

    return 0;
}

上述代码在mingw7.3.0编译后,输出如下:

0x61fea0:type : 1; name : a; rest : 2
----------------
0x61fe64:type : 1; name : a; rest : 2
0xeb8080:type : 33; name : a; rest : 999
----------------
0x61fe64:type : 1; name : a; rest : 2
0x61fe94:type : 1; name : e; rest : 999

可以看到,构造函数中调用构造函数,实际上会多产生一个实例,这可能不是一个严重问题,但如果要构造大量实例,那么还是值得注意的。

C++11标准有一个特性:委托构造函数。这个特性可以在减少冗余代码的同事,避免上边的问题。

#include <iostream>
using namespace std;

class Info{
public:
    Info() { Init(); printInfo();};
    Info(int i) : Info() { type = i; printInfo();}
    Info(char e) : Info() { name = e;  printInfo();}

private:
    void Init() {rest = 2;}
    void printInfo() {cout << this << ":" << "type : " << type << "; name : " << name << "; rest : " << rest << endl;}
    int type {1};
    char name {'a'};
    int rest {999};
};

int main()
{
    Info info;
    cout << "----------------" << endl;
    Info Info2(33);
    cout << "----------------" << endl;
    Info info3('e');

    return 0;
}

这个版本的输出:

0x61feb4:type : 1; name : a; rest : 2
----------------
0x61fea8:type : 1; name : a; rest : 2
0x61fea8:type : 33; name : a; rest : 2
----------------
0x61fe9c:type : 1; name : a; rest : 2
0x61fe9c:type : 1; name : e; rest : 2

进一步,为了应用初始化列表(因为初始化列表的初始化方式总是先于构造函数完成的。实际上在编译时就完成),还可以进行如下修改。

class Info{
public:
    Info(): Info(1, 'a'){  printInfo();}
    Info(int i) : Info(i, 'a') { type = i; printInfo(); }
    Info(char e) : Info(1, e) { name = e;  printInfo();}
private:
    Info(int i, char e) : type(i), name(e) { Init();}
    void Init() {rest = 2;}
    void printInfo() {cout << this << ":" << "type : " << type << "; name : " << name << "; rest : " << rest << endl;}
    int type {1};
    char name {'a'};
    int rest {999};
};

总结:问题没有爆发出来,不能说明没有问题。

委派构造的一个很实际的应用就是使用构造模板函数产生目标构造函数。

class Constructed {
    template<class T> Constructed(T first, T last) : l(first, last) {}
    list<int> l;
public:
    Constructed(vector<short> & v) : Constructed(v.begin(), v.end()) {}
    Constructed(deque<int> & d) : Constructed(d.begin(), d.end()) {}
};

此外,在异常处理方面,如果在委派构造函数中使用try的话,那么从目标构造函数中产生的异常,都可以在委派构造函数中被捕捉到。

#include <iostream>
using namespace std;

class Except {
public:
   Except(double d) try : Except(1, d)
        {
            cout << "normal" << endl;
            //...
        }
        catch(...) {
            cout << "exception." << endl;
        }
private:
    Except(int i, double d) {
        type = i;
        data = d;
        cout << "throw 0" << endl;
        throw 0;
    }
    int type;
    double data;
};

int main()
{
    Except a(1.0);
    return 0;
}

输出

throw 0
exception.
terminate called after throwing an instance of 'int'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值