2016-09-28 91 views
1

我正在学习初始化程序列表,并获悉必须使用它初始化const成员,因为您无法使用默认构造函数或参数化构造函数初始化它。为什么没有其他构造函数被允许?

class Foo 
{ 
private: 
    const int y;    
public:   
    Foo(int yy) :y(yy){}   
    int getY(); 
}; 

现在假设,如果我有另一个成员int x;不是一个常量,为什么我不能初始化使用默认的构造函数它,这是什么限制背后的想法?

,让错误代码:

class Foo 
{ 
private: 
    const int y;  
    int x; 
public:  
    Foo(int yy) :y(yy){} 
    Foo() 
    { 
     x = 100; 
    } 
    int getY(); 
}; 
+4

“_Why为什么我不能使用默认构造函数初始化它?”是的,你可以吗?你能更清楚一点吗? –

+2

您可以在默认构造函数中初始化您喜欢的任何成员。我不明白这个问题。请发表一个你认为不*工作的MCVE。 –

+0

Btw,*“因为您不能使用默认构造函数或参数化构造函数来初始化它。”*听起来像对“默认构造函数”这个术语有误解。 –

回答

4

I am learning about initializer list and learnt that const members must be initialized using it, because you cannot initialize it using default constructor or parameterised constructor.

const的成员可以在这两个默认构造函数和任何parametrised构造函数的成员初始化列表中初始化。 (默认构造函数是可以不带参数调用构造函数)。

Now suppose if I have another member int x; not a const,Why can't I initialize it using default constructor, What is the idea behind this restriction?

您可以初始化任何数量的成员(有可能是一些实现定义的限制,但它不是有关这个问题),在默认构造方法。没有你描述的这种限制。

演示,类两个成员,无论是在默认构造函数初始化:

struct Foo { 
    const int y; 
    int  x; 
    Foo(): y(1), x(100){} 
}; 

编辑为MCVE。

The code that gives error:

class Foo 
{ 
private: 
    const int y;  
    int x; 
public:  
    Foo(int yy) :y(yy){} 
    Foo() 
    { 
     x = 100; 
    } 
    int getY(); 
}; 

所有构造函数必须初始化const的成员。您的参数化构造函数会初始化y,但默认构造函数不会。这就是为什么它不起作用。看到我的演示上面的工作示例。

PS。您的参数化构造函数不会初始化x,但这是正确的:x不是常量,因此您可以稍后为其分配值。


In my code if I have a parameterised constructor like Foo(int xx) { x = xx;}

It will not give any error

此程序:

struct Foo { 
    const int y; 
    int  x; 
    Foo(int xx) { x = xx;} 
}; 

病形成在标准C++。如果您的编译器在没有警告的情况下接受它,那么它不符合标准。

+1

*“可能有一些实现定义的限制,但与此问题无关”*当天有趣的事实:C++ 14的附录B建议此限制至少为6144. –

+0

您错了我的朋友。 –

+0

当你说所有的构造函数都必须初始化const成员。 –

1

不清楚你想完成什么。 下面的代码只是编译

class Foo 
{ 
private: 
    int _nonConst; 
    const int _const; 

public: 
    Foo() : _const(10), _nonConst(11) 
    { 

    } 

    Foo(int x) : _const(x), _nonConst(x) 
    { 

    } 
}; 
0

我不知道我正确的理解这个问题。总结一下你能做什么,不能做什么:

// .h 
class MyClass 
{ 
private: 
    int x; 
    const int y; 

public: 
    MyClass(); 
    MyClass(int initValue); 
}; 

// .cpp 
// The following is legal 
MyClass::MyClass() 
: x(0), y(0) 
{}  

// Alternatively, the following is legal too. 
MyClass::MyClass() 
: y(0) 
{ 
    x = 0; 
} 

// This is not legal: y cannot be initialized that way as it is const. 
// No problem for x though. 
MyClass::MyClass() 
{ 
    x = 0; 
    y = 0; // You cannot do that 
} 

// The following is legal 
MyClass::MyClass(int initValue) 
: x(initValue), y(initValue) 
{}  

// Alternatively, the following is legal too. 
MyClass::MyClass(int initValue) 
: y(initValue) 
{ 
    x = initValue; 
} 

// This is not legal: y cannot be initialized that way as it is const. 
// No problem for x though. 
MyClass::MyClass(int initValue) 
{ 
    x = initValue; 
    y = initValue; // You cannot do that 
} 

当然,你只能有一个CTor或每个声明的签名。上面的例子是3种选择 - 每个声明的签名有2个合法的和1个非法的。

相关问题