C++ 的intialization list 和assignment

[补充]

如果一个类的所有成员都是public的,而且没有默认构造函数,就可以使用intialization list对类的成员进行初始化。

 

三大法

 

#include <iostream>
#include <map>
using namespace std;

class A
{
public:
    A(int a):i(a),j(a){
        //i = a;   // i is not modifiable here
    }
    const int i;
    int & j;
};

class B:public A
{
public:
    B():A(0)
    {
    }

    B(int a):A(a) // B's base class A only has one constructor A(int a), so you must provide a way to initilize A's parameters;
    {
    }
}

 

原文链接: https://www.cnblogs.com/whyandinside/archive/2012/09/17/2689315.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ 的intialization list 和assignment

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/63049

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午10:43
下一篇 2023年2月9日 上午10:44

相关推荐