“const class”是什么意思? 意思是?
经过一些查找和替换重构后,我最终得到了这个宝石:
const class A
{
};
“const class”是什么意思? 看来编译没问题。
After some find and replace refactoring I ended up with this gem:
const class A
{
};
What does "const class" mean? It seems to compile ok.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在该示例中,
const
毫无意义,您的编译器应该给您一个错误,但如果您使用它在结束}
和之间声明该类的变量, >;
,然后将这些实例定义为const
,例如:The
const
is meaningless in that example, and your compiler should give you an error, but if you use it to declare variables of that class between the closing}
and the;
, then that defines those instances asconst
, e.g.:不适合我。 我认为你的编译器只是出于礼貌而忽略了它。
编辑:是的,GCC 抱怨说,VC++ 默默地忽略了 const。
Not for me it doesn't. I think your compiler's just being polite and ignoring it.
Edit: Yep, VC++ silently ignores the const, GCC complains.
如果你有这个:
那么它显然意味着 'a' 是 const。 否则,我认为它可能是无效的c++。
If you had this:
Then it would clearly mean that 'a' is const. Otherwise, I think that it is likely invalid c++.
除非您随后声明该类的实例,否则它毫无意义,例如以下示例:
如果您正在等待 C++0x,则为临时
nullptr
实现。It's meaningless unless you declare an instance of the class afterward, such as this example:
An interim
nullptr
implementation if you're waiting for C++0x.尝试用 GCC 编译它,它会给出以下错误:
错误:只能为对象和函数指定限定符。
从错误中可以看出,只有对象(变量、指针、类对象等)和函数可以是常量。 因此,尝试将对象设置为常量,然后它应该可以正常编译。
const class A {};
const A a;
Try compiling it with GCC, it will give you below error:
error: qualifiers can only be specified for objects and functions.
As you can see from the error that only objects(variables, pointers, class objects etc.) and functions can be constant. So try making the object as constant, then it should compile fine.
const class A {};
const A a ;