久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 const 成員函數返回非常量引用

Returning non-const reference from a const member function(從 const 成員函數返回非常量引用)
本文介紹了從 const 成員函數返回非常量引用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

為什么返回對指向的成員變量的引用有效,而另一個無效?我知道 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 成員函數返回非常量引用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 一区二区三区免费 | 欧美不卡一区二区 | 国产精品一区二区三级 | 一级亚洲| 日本一区二区电影 | 久久精品一区二区视频 | 亚洲精品免费在线观看 | 玖玖综合网| 日本黄色大片免费 | 成人午夜免费网站 | 亚洲一区成人 | 精品久久国产视频 | 日韩久久精品 | 国产精品永久久久久 | 成人欧美一区二区三区色青冈 | 精品国产乱码久久久久久牛牛 | 久久国产精品久久国产精品 | 国产日韩欧美在线播放 | 狠狠爱免费视频 | 久久这里只有精品首页 | 91成人在线| 亚洲毛片| 国产成人久久精品一区二区三区 | 国产69久久精品成人看动漫 | 久久这里有精品 | 色毛片 | 在线免费av电影 | 国产一区成人 | 精品国产乱码久久久久久影片 | 精品免费国产视频 | 免费视频久久 | 日本二区在线观看 | 岛国精品| 亚洲精品字幕 | 久久99精品久久久 | av中文网| 91久久精品国产 | 无人区国产成人久久三区 | 国产久| 欧美日韩国产一区 | 国产午夜在线 |