2008-10-16 106 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。