加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何使用成员的非默认构造函数?

发布时间:2020-12-16 07:52:41 所属栏目:百科 来源:网络整理
导读:我有两堂课 class a { public: a(int i);};class b { public: b(); //Gives me an error here,because it tries to find constructor a::a() a aInstance;} 我如何得到它,以便aInstance被实例化(int i),而不是尝试搜索一个默认的构造函数?基本上,我想控制从
我有两堂课
class a {
    public:
        a(int i);
};

class b {
    public:
        b(); //Gives me an error here,because it tries to find constructor a::a()
        a aInstance;
}

我如何得到它,以便aInstance被实例化(int i),而不是尝试搜索一个默认的构造函数?基本上,我想控制从b的构造函数中调用a的构造函数.

解决方法

您需要在构造函数初始化程序列表中显式调用(int):
b() : aInstance(3) {}

其中3是您要使用的初始值.虽然可以是任何int请参阅有关订单和其他注意事项的重要说明的意见.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读