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;
    {
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM