2010-10-29 98 views
2

这里是简单的程序。 如果我评论构造函数,我得到一个错误 只是想检查是什么原因?关于const限定符和构造函数的问题

t.cc: In function 'int main(int, char**)':                                
t.cc:26: error: uninitialized const 'const_test' 


#include <iostream>                                      

using namespace std;                                      

class TestPrint                                       
{                                          
public:                                         
    // TestPrint() {}                                      
    void Print()                                       
    {                                          
    std::cout << "TestPrint" << std::endl;                                
    }                                          

    void Print() const                                      
    {                                          
    std::cout << "const TestPrint" << std::endl;                               
    }                                          
};                                          


int main(int argc, char* argv[])                                   
{                                          
    TestPrint normal_test;                                     
    normal_test.Print();                                     

    const TestPrint const_test;                                   
    const_test.Print();                                     
}                                
+0

你没有说出什么错误 – Sheen 2010-10-29 15:52:28

+0

我做到了!这里是它 - t.cc:在函数'int main(int,char **)': t.cc:26:error:未初始化const'const_test' – Prafulla 2010-10-29 15:53:24

+0

在Visual Studio 2005中没有错误 – Sheen 2010-10-29 15:56:50

回答

2

根据ISO标准(8.5 [dcl.init]段落9):

如果对象没有指定初始化,并且对象是 (可能CV修饰)非POD类类型(或其数组), 对象应该被默认初始化;如果对象为const限定类型 ,则基础类类型应具有用户声明的默认构造函数 。

所以海湾合作委员会就在这里。对不起,VC家伙。

+0

不要后悔。我们可以忍受它。 :D – Sheen 2010-10-29 16:24:04

+0

@Sheen:开个玩笑吧。这不是有史以来最糟糕的问题。 =) – vitaut 2010-10-29 16:32:03

1

您的代码在Microsoft Visual Studio 2008中编译。也许这是您的编译器的错误,您使用的编译器是什么?

2

一个const限定的对象必须在被定义的地方被初始化;通过初始化程序(例如const TestPrint const_test = TestPrint();)或默认的构造函数。此规则适用于所有对象,即使它们没有任何要初始化的数据成员。

所以没有默认的构造函数,你的代码是不合格的;用它,它很好,默认的构造函数用于初始化。

6

确实是不合格的。 §8.5/ 9:

If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a nonstatic object, the object and its subobjects, if any, have an indeterminate initial value; if the object or any of its subobjects are of const-qualified type, the program is ill-formed.

重点是我的。任何不会为您的程序发布诊断的编译器都不符合规范(查看您的MSVC)。一个更简单的测试:

struct foo {}; 

int main() 
{ 
    const foo f; 
} 

这个想法很简单:常量需要初始化为某些东西。如果你没有用户定义的构造函数,你没有初始化。

+0

您的示例不合格的原因不是因为缺少用户声明的默认构造函数,而是因为缺少结构的大括号初始化器。请注意'struct foo'是一个POD结构,它不需要构造函数。 – spockwang 2011-08-04 06:18:05

+0

@wbb:我不打算把它看作是一种if-only-only-if。 – GManNickG 2011-08-04 08:32:34