2008-10-16 219 views

回答

30

什麼是 「常量類」 是什麼意思?它似乎編譯好。

不適合我沒有。我認爲你的編譯器只是禮貌而忽略它。 GCC抱怨,VC++默默忽略const,GCC抱怨。

+3

我會說,它看起來像VC++越來越錯誤的邊緣情況 – 2008-10-16 00:31:02

+0

貌似; 'const'不影響我能找到的任何東西。 – 2008-10-16 00:34:16

17

如果你有這樣的:

然後,它顯然意味着 'A' 是常量。否則,我認爲這可能是無效的C++。

+0

C++的好老被遺忘的功能!我記得在很久以前你在做C這樣的東西。 – 2008-10-16 01:47:13

41

const在這個例子毫無意義,你的編譯器應該給你一個錯誤,但如果你用它來關閉};之間聲明類的變量,然後定義這些實例作爲const,如:


const class A 
{ 
public: 
    int x, y; 
} anInstance = {3, 4}; 

// The above is equivalent to: 
const A anInstance = {3, 4}; 
7

這是毫無意義的,除非你聲明類的一個實例後,像這樣的例子:

const // It is a const object... 
class nullptr_t 
{ 
    public: 
    template<class T> 
     operator T*() const // convertible to any type of null non-member pointer... 
    { return 0; } 

    template<class C, class T> 
    operator T C::*() const // or any type of null member pointer... 
    { return 0; } 

    private: 
    void operator&() const; // Can't take address of nullptr 

} nullptr = {}; 

中期nullptr實現,如果你正在等待C++ 0x。