c++构造函数

1.有些成员必须在构造函数初始化列表中进行初始化,在构造函数体重对他们不起作用。例如没有默认构造函数的类成员,以及const或引用类型的成员

#pragma once
class ConstRef
{
public:
    ConstRef(int x);
//    ~ConstRef(void);
private:
    const int i;
    int ia;
    int& ib;
};

ConstRef::ConstRef(int x):i(x),ib(x)
{
    //i = x;    //error
    ia = x;
    //ib = x; /error
}

2.构造函数中成员初始化顺序和成员声明的顺序是一样的

posted @ 2012-10-19 14:41  恒月美剑  阅读(157)  评论(0编辑  收藏  举报