为什么要加一个“const”在最后?

发布于 2024-10-23 20:34:25 字数 416 浏览 1 评论 0原文

可能的重复:
C++ const 在类方法中的使用
C++ 方法声明中最后一个“const”的含义?

int operator==(const AAA &rhs) const;

这是一个运算符重载声明。为什么要把const放在最后呢?谢谢

Possible Duplicates:
c++ const use in class methods
Meaning of “const” last in a C++ method declaration?

int operator==(const AAA &rhs) const;

This is a operator overloading declaration. Why put const at the end? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

孤檠 2024-10-30 20:34:25

const 关键字表示该方法不会更改对象。由于 operator== 用于比较,因此无需更改任何内容。因此,它是const。对于像 operator= 这样确实修改对象的方法,必须省略它。

它可以让编译器仔细检查您的工作,以确保您没有做任何不应该做的事情。有关更多信息,请查看 http://www.parashift.com/c++- faq-lite/const- Correctness.html

The const keyword signifies that the method isn't going to change the object. Since operator== is for comparison, nothing needs to be changed. Therefore, it is const. It has to be omitted for methods like operator= that DO modify the object.

It lets the compiler double-check your work to make sure you're not doing anything you're not supposed to be doing. For more information check out http://www.parashift.com/c++-faq-lite/const-correctness.html.

浅笑轻吟梦一曲 2024-10-30 20:34:25

创建一个方法const将让该类的常量对象调用它。因为此方法无法更改对象的任何成员(编译器错误)。

值得一提的是,const 是方法签名的一部分,因此在同一个类中,您可能有两个具有相同原型的方法,但一个是 const,另一个不是。在这种情况下,如果从变量对象调用重载方法,则调用非常量方法,如果从常量对象调用重载方法,则调用 const 方法。

但是,如果您只有一个 const 方法(没有非常量重载),那么它会从变量和常量对象中调用。

例如:

#include <iostream>

using std::cout;

class Foo
{
public:
    bool Happy;
    Foo(): Happy(false)
    {
        // nothing
    }
    void Method() const
    {
        // nothing
    }
    void Method()
    {
        Happy = true;
    }
};

int main()
{
    Foo A;
    const Foo B;
    A.Method();
    cout << A.Happy << '\n';
    B.Method();
    cout << B.Happy << '\n';
    return 0;
}

将输出:

1
0
Press any key to continue . . .

Making a method const will let a contant object of the class to call it. because this method cannot change any of the object's members (compiler error).

It may deserve mention that const is a part of the method's signature, so in the same class you may have two methods of the same prototype but one is const and the other isn't. In this case, if you call the overloaded method from a variable object then the non-const method is called, and if you call it from a constant object then the const method is called.

However, if you have only a const method (there is no non-const overload of it), then it's called from both variable and constant object.

For Example :

#include <iostream>

using std::cout;

class Foo
{
public:
    bool Happy;
    Foo(): Happy(false)
    {
        // nothing
    }
    void Method() const
    {
        // nothing
    }
    void Method()
    {
        Happy = true;
    }
};

int main()
{
    Foo A;
    const Foo B;
    A.Method();
    cout << A.Happy << '\n';
    B.Method();
    cout << B.Happy << '\n';
    return 0;
}

Will output :

1
0
Press any key to continue . . .
谢绝鈎搭 2024-10-30 20:34:25

任何 C++ 声明末尾的“const”告诉编译器它不会改变它所属的东西。

"const" at the end of any c++ declaration tells the compiler that it won't change the thing it belongs to.

胡大本事 2024-10-30 20:34:25

这将方法本身标记为常量,这意味着只要您拥有对相关对象的 const 引用,编译器就允许您使用该方法。如果您有 const 引用,则只能调用也声明为 const 的方法。

That marks the method itself as being constant, which means the compiler will permit you to use the method whenever you have a const reference to the relevant object. If you have a const reference, you otherwise can only invoke methods that are also declared as const.

混浊又暗下来 2024-10-30 20:34:25

方法声明末尾的“const”将该方法标记为可以安全地调用常量对象。

A 'const' at the end of a method declaration marks that method as being safe to call on a constant object.

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