从 const 成员函数返回非常量引用

2023-02-22C/C++开发问题
2

本文介绍了从 const 成员函数返回非常量引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

为什么返回对指向的成员变量的引用有效,而另一个无效?我知道 const 成员函数应该只返回 const 引用,但为什么对于指针来说这似乎不是真的?

Why does returning the reference to a pointed-to member variable work, but not the other? I know that a const member function should only return const references, but why does that not seem true for pointers?

class MyClass
{
  private:
    int * a;
    int b;
  public:
    MyClass() { a = new int; }
    ~MyClass() { delete a; }

    int & geta(void) const { return *a; } // good?
    int & getb(void) const { return b; }  // obviously bad
};

int main(void)
{
  MyClass m;

  m.geta() = 5;  //works????
  m.getb() = 7;  //doesn't compile

  return 0;
}

推荐答案

int & geta(void) const { return *a; } // good?
int & getb(void) const { return b; }  // obviously bad

在一个 const 函数中,每个数据成员都变成了 const 这样它就不能被修改.int 变为 const intint * 变为 int * const,依此类推.

In a const-function, every data member becomes const in such way that it cannot be modified. int becomes const int, int * becomes int * const, and so on.

因为在你的第一个函数中 atype 变成了 int * const,而不是 const int *>,因此您可以更改数据(可修改):

Since the type of a in your first function becomes int * const, as opposed to const int *, so you can change the data (which is modifiable):

  m.geta() = 5;  //works, as the data is modifiable

区别:const int*int * const.

  • const int* 表示指针是非常量,但指针指向的数据是const.
  • int * const 表示指针是const,但指针指向的数据是非常量.
  • const int* means the pointer is non-const, but the data the pointer points to is const.
  • int * const means the pointer is const, but the data the pointer points to is non-const.

你的第二个函数试图返回 const int &,因为 btype 变成了 const int.但是您已经在代码中将实际返回类型提到为 int &,因此该函数甚至不会 编译(请参阅 this),无论您在 main() 中做什么,因为返回的 type 不匹配.这是修复:

Your second function tries to return const int &, since the type of b become const int. But you've mentioned the actual return type in your code as int &, so this function would not even compile (see this), irrespective of what you do in main(), because the return type doesn't match. Here is the fix:

 const int & getb(void) const { return b; }  

现在它编译正常!.

这篇关于从 const 成员函数返回非常量引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6