gpt4 book ai didi

c++ - C++生命周期延长的问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:06:31 26 4
gpt4 key购买 nike

我试图理解 C++ 临时对象生命周期延长的语义。我试图模拟简单的情况,但有点惊讶。

下面我提供我的代码。

#include <iostream>

struct C
{
C(const int new_a) { a = new_a; };

int a = 0;
};

C return_num()
{
C num(20);

std::cout << "From func(): num = " << num.a << ", by adress: " << &num.a << std::endl;

return num;
}

void pass_num(const C& num)
{
std::cout << "From func(): num = " << num.a << ", by adress: " << &num.a << std::endl;
}

int main()
{
std::cout << "\nLifetime extention:" << std::endl;
{
const C& ext_num = return_num();

std::cout << "From main(): num = " << ext_num.a << ", by adress: " << &ext_num.a << std::endl;
}

std::cout << "\nPassing by reference:" << std::endl;
{
C num(20);

std::cout << "From main(): num = " << num.a << ", by adress: " << &num.a << std::endl;

pass_num(num);
}
}

这里是主要问题:return_num()从我的角度来看,工作很奇怪,因为我期望变量的地址,我试图在 main 中输出它, 将与 return_num() 中的内部相同.你能解释一下为什么不是吗?

例如 pass_num()输出地址匹配我在 main 中得到的外部地址.

这是示例输出:

生命周期延长:
从 func(): num = 20, by address: 0x7fff44fc8b4c
从 main(): num = 20, by address: 0x7fff44fc8b70

通过引用:
从 main(): num = 20, by address: 0x7fff44fc8b6c
从 func(): num = 20, by address: 0x7fff44fc8b6c

最佳答案

移动构造函数通常“窃取”参数持有的资源(例如指向动态分配对象、文件描述符、TCP 套接字、I/O 流、运行线程等的指针)而不是复制它们,然后离开一些有效但不确定状态的参数。

Please see Move Constructor

我更改了您代码中的以下内容,希望它能按预期工作。我将 int a 更改为 int* a

#include <iostream>

class C
{
public:
int *a;
C( int new_a)
{
a = new int();
*a = new_a;
};
C(const C& rhs) { std::cout << "Copy " << std::endl; this->a = rhs.a; }
C(C&& rhs):a(std::move(rhs.a))
{
std::cout << "Move!!" <<"Address resource a " << &(*a) << ", Address of
resource rhs.a" << &(*rhs.a) << std::endl; rhs.a = nullptr;
std::cout << "Value of a:: " << *a << std::endl;
}

};

C return_num()
{
C num(20);

std::cout << "From return_num(): num = " << *num.a << ", Address of resource a :
"<< &(*num.a)<< std::endl;
return (std::move(num));
}

void pass_num(const C& num)
{
std::cout << "From pass_num(): num = " << *num.a << ", by adress: " << &num.a <<
std::endl;
}

int main()
{
std::cout << "\nLifetime extention:" << std::endl;
{
const C& ext_num = return_num();

std::cout << "From main() 1 : num = " << *(ext_num.a) << ", by resource
adress: " << &(*ext_num.a) << std::endl;
}

std::cout << "\nPassing by reference:" << std::endl;
{
C num(20);

std::cout << "From main() 2 : num = " << *num.a << ", by adress: " << &num.a
<< std::endl;

pass_num(num);
}
return 0;
}

以上代码产生以下输出:

Lifetime extention:
From return_num(): num = 20, Address of resource a : 0x7fffeca99280
Move!!Address resource a 0x7fffeca99280, Address of resource rhs.a0x7fffeca99280
Value of a:: 20
From main() 1 : num = 20, by resource adress: 0x7fffeca99280

Passing by reference:
From main() 2 : num = 20, by adress: 0x7ffff466f388
From pass_num(): num = 20, by adress: 0x7ffff466f388

希望对您有所帮助!

关于c++ - C++生命周期延长的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56750030/

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