“const class”是什么意思? 意思是?

发布于 2024-07-06 14:07:35 字数 116 浏览 7 评论 0原文

经过一些查找和替换重构后,我最终得到了这个宝石:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晨曦慕雪 2024-07-13 14:07:35

在该示例中,const 毫无意义,您的编译器应该给您一个错误,但如果您使用它在结束 }之间声明该类的变量, >;,然后将这些实例定义为 const,例如:


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

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

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 as const, e.g.:


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

// The above is equivalent to:
const A anInstance = {3, 4};
九局 2024-07-13 14:07:35

“const 类”是什么意思? 看来编译没问题。

不适合我。 我认为你的编译器只是出于礼貌而忽略了它。

编辑:是的,GCC 抱怨说,VC++ 默默地忽略了 const。

What does "const class" mean? It seems to compile ok.

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.

清泪尽 2024-07-13 14:07:35

如果你有这个:

const class A
{
} a;

那么它显然意味着 'a' 是 const。 否则,我认为它可能是无效的c++。

If you had this:

const class A
{
} a;

Then it would clearly mean that 'a' is const. Otherwise, I think that it is likely invalid c++.

舞袖。长 2024-07-13 14:07:35

除非您随后声明该类的实例,否则它毫无意义,例如以下示例:

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 = {};

如果您正在等待 C++0x,则为临时 nullptr 实现。

It's meaningless unless you declare an instance of the class afterward, such as this example:

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 = {};

An interim nullptr implementation if you're waiting for C++0x.

浅暮の光 2024-07-13 14:07:35

尝试用 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 ;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文