gpt4 book ai didi

c++ - 在 C++ 中返回此指针的函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:42 24 4
gpt4 key购买 nike

第一个代码:

#include <iostream>
using namespace std;
class demo
{
int a;
public:
demo():a(9){}
demo& fun()//return type isdemo&
{
return *this;
}
};

int main()
{
demo obj;
obj.fun();
return 0;
}

第二个代码:

#include <iostream>
using namespace std;
class demo
{
int a;
public:
demo():a(9){}
demo fun()//return type is demo
{
return *this;
}
};

int main()
{
demo obj;
obj.fun();
return 0;
}

这两个代码之间有什么区别,因为它们都在 gcc 中工作?我是新来的,如果我的提问方式有误,请原谅我。

最佳答案

demo & fun() 返回对当前对象的引用。 demo fun() 返回一个对象,该对象是通过复制当前对象生成的。

关于c++ - 在 C++ 中返回此指针的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5729336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com