const 成员函数不允许什么?

发布于 2024-09-11 07:41:39 字数 571 浏览 1 评论 0原文

class A{
private:
    int a;
public:
    A() {a = 4;}
    const int& random1() const {return a; }
    //int&     random2() const {return a; }
    const int* random3() const {return &a;}
    //int*     random4() const {return &a;}
};

int main(){
    A objA;
    cout<<objA.random1()<<"\n";
    cout<<*objA.random3()<<"\n";
}

如上所述,不允许使用 random2()random4()。不知何故,我一直都知道这一点,但直到今天,我在编写自己的代码时才遇到它。

除了这两种情况之外,还有哪些情况在 const 成员函数中是不允许的?

对 C++ 标准文本的任何引用也会有所帮助。谢谢!

class A{
private:
    int a;
public:
    A() {a = 4;}
    const int& random1() const {return a; }
    //int&     random2() const {return a; }
    const int* random3() const {return &a;}
    //int*     random4() const {return &a;}
};

int main(){
    A objA;
    cout<<objA.random1()<<"\n";
    cout<<*objA.random3()<<"\n";
}

random2() and random4() are not permitted as defined above. I somehow knew this all along but never came across it while writing my own code, until today.

What all except these two cases is not permitted in const member functions?

Any reference to C++ standard text will also be helpful. Thanks!

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

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

发布评论

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

评论(2

浪推晚风 2024-09-18 07:41:39

首先要明白const T*是一个指向某些不能改变的T的指针。要记住的第二件事是所有成员实际上都是通过 this-> 访问的。

所以(§9.3.1):

非静态成员函数可以声明为 const、volatile 或 const volatile。这些 cv 限定符影响 this 指针的类型 (9.3.2)。

它的作用(§9.3.2):

在非静态 (9.3) 成员函数体中,关键字 this 是一个非左值表达式,其值是调用该函数的对象的地址。类X的成员函数中this的类型是X*。如果成员函数声明为 const,则 this 的类型为 const X*;如果成员函数声明为 volatile,则 this 的类型为 volatile X*;如果成员函数声明为 const volatile,则 this 的类型为 const不稳定的 X*。

函数上的 const 使 this 指针成为 const T*

这就是这些示例失败的原因:在 int& 变体中,a 的访问方式为 this->a, this 是 const T*,因此 aconst int。并且const int不能隐式转换为int&。与其他功能相同。

换句话说,当一个函数是 const 时,它会在类中的所有内容上添加一个 const,并且您不能隐式地抛弃该 const 。

First understand that const T* is a pointer to some T that cannot be changed. The second thing to remember is all members are actually accessed via this->.

So (§9.3.1):

A nonstatic member function may be declared const, volatile, or const volatile. These cvqualifiers affect the type of the this pointer (9.3.2).

And what it does (§9.3.2):

In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

A const on a function makes the this pointer const T*.

This is why those examples fail: In the int& variant, a is accessed as this->a, this is const T*, so a is a const int. And const int cannot be implicitly converted to int&. Same with the other function.

In other words, when a function is const it smacks a const on everything in the class, and you can't implicitly cast the const away.

遗弃M 2024-09-18 07:41:39

Const 成员函数不能调用非常量成员函数,即使它们不更改任何成员数据。有时您需要提供同一函数的 const 和非 const 版本,因为 this 指针隐式传递给成员函数并在重载决策中发挥作用。

Const member functions can't call non-const member function, even if they don't change any member data. Sometimes you need to provide both const and non-const versions of the same function, because this pointer is implicitly passed to member functions and plays a role in overload resolution.

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