2010-10-29 99 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