简体   繁体   English

具有参考返回类型的C ++类成员函数

[英]C++ class member function with reference return type

I am a newbie to programming and C++. 我是编程和C ++的新手。 I'm confused how reference works differently here. 我很困惑这里引用如何工作。

The code below outputs 0 5 but removing the & from A &GotObj() would output 0 0 . 下面的代码输出0 5但是从A &GotObj()删除&将输出0 0 Why didn't the second case output 0 5 ? 为什么第二种情况不输出0 5

Thanks in advance. 提前致谢。

#include <iostream>
using namespace std;

class A {
public:
    int val;
    A(int n=0) {
        val = n;

    }
    A &GetObj() {
        return *this;
    }
};

int main() {
    A a;
    cout << a.val << endl;
    a.GetObj() = 5;
    cout << a.val << endl;
    return 0;
}

When you return by value the expression a.GetObj() just creates a temporary. 当按值返回时,表达式a.GetObj()只会创建一个临时值。 Assigning then only changes the value of that temporary. 然后分配仅更改该临时值。

This of a reference as just a pointer with a slightly different syntax. 这个引用只是一个语法稍有不同的指针。 When GetObj is defined as GetObj定义为

A& GetObj() { return *this: }

it returns a reference (ie a pointer) to a. 它返回对a的引用(即指针)。 Then, the assignment 然后,分配

a.GetObj() = 5

effectively invokes the assignment operator of a with the argument 5, which changes the value of a to 5. 有效地调用的赋值运算符a用参数5,其改变的一个至5的值。

But if you define GetObj() as 但是,如果您将GetObj()定义为

A GetObj { return *this; }

it returns a completely new temporary object that has the same value as a. 它返回一个与a值相同的全新临时对象。 So when you later assign 5 to it, it doesn't change the value of a. 因此,当您以后为其分配5时,它不会更改a的值。

Case 1: A GetObj() 情况1:一个GetObj()

  • lets Create object of Class A : 让Create类为A的对象:

A a; A a;

  • suppose this objects memory location is 0x87965432 . 假设此对象的存储位置为0x87965432 Now, when you call function of object a by a.GetObj() then it returns a temporary object whose memory location will be completely different then Class A's object a, lets say 0x98672345 . 现在,当您通过a.GetObj()调用对象a的函数时,它将返回一个临时对象,该对象的存储位置将与A类的对象a完全不同,可以说0x98672345 now if you assign value 5 by 现在,如果您将值5指定为

a.GetObj() = 5 a.GetObj()= 5

  • then you are assigning value 5 to the object located at memory location 0x98672345 . 然后您将值5分配给位于内存位置0x98672345的对象。 now, you are going to print val variable of object which is located at memory location 0x87965432. 现在,您将要打印位于内存位置0x87965432的对象的val变量。 thats why it will print 0. 这就是为什么它将打印0的原因。

Case 2: A &GetObj() 情况2:A&GetObj()

  • as explained in Case 1, if you create object by 如案例1所述,如果您通过以下方式创建对象

A a; A a;

  • lets consider its memory location 0x87965432 . 让我们考虑一下它的内存位置0x87965432 now when you call function GetObj, by a.GetObj(), then return object will be of same memory location 0x87965432 , as its concept of return by Referance. 现在,当您通过a.GetObj()调用函数GetObj时,返回对象将具有相同的存储位置0x87965432 ,这是其通过引用返回的概念。 now assigning value 5 to this object, 现在将值5分配给该对象,

a.GetObj() = 5 a.GetObj()= 5

will reflect effect in object a. 将反映对象a的效果。 and now printing value of variable val will be as expected 5. 现在变量val的打印值将达到预期的5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM