【发布时间】:2021-01-20 19:34:00
【问题描述】:
我有一些类A,我可以不使用任何东西构造它,也可以使用std::function。在销毁时,应该调用给定的函数(如果有的话)。我的问题是对象在创建并由我的getSomeA() 函数返回后立即被销毁,该函数在应该调用它之前调用std::function。传递给构造函数的函数只能调用一次。一些示例代码:
#include <iostream>
#include <functional>
static void testFunction(const std::string& msg)
{
std::cout << "TestFunction: " << msg << "\n";
}
class A {
public:
A(void) = default;
A(const std::function<void()>& onDestroy) :
onDestroy(onDestroy)
{ }
~A(void)
{
if (onDestroy) onDestroy();
else std::cout << "in dtor but no onDestroy was set\n";
}
private:
std::function<void()> onDestroy;
};
A getSomeA(void)
{
return A(std::bind(testFunction, "the A that was created inside getSomeA"));
}
int main(void)
{
A b1;
std::cout << "After creating b1\n";
b1 = getSomeA();
std::cout << "After reassigning b1\n";
std::cout << "Here the program works with b1...\n";
}
程序输出
After creating b1
TestFunction: the A that was created inside getSomeA
After reassigning b1
Here the program works with b1...
TestFunction: the A that was created inside getSomeA
所以函数在它应该被调用之前被调用(在 int main() 的末尾)。
添加移动构造函数和赋值运算符后,一切都按预期工作:
A(A&& other) :
onDestroy(std::exchange(other.onDestroy, nullptr))
{ }
A& operator=(A&& other)
{
onDestroy = std::exchange(other.onDestroy, nullptr);
return *this;
}
程序输出
After creating b1
in dtor but no onDestroy was set
After reassigning b1
Here the program works with b1...
TestFunction: the A that was created inside getSomeA
实际问题:第一次销毁对象以便调用 testFunction?在getSomeA() 或在主函数中赋值之前但在getSomeA() 中创建对象之后?
所有编辑:我试图将我的问题搁置一小时,但由于我不知道 C++ 中的移动/复制语义,这对我来说非常困难。
【问题讨论】:
-
你能把这个问题简化一下吗?方便我们提供帮助。目前有很多内容要阅读。
-
好的,我会试试的。
-
A& operator=(A other)是复制赋值运算符,而不是移动赋值运算符。 -
移动语义不会改变对象的生命周期,只会管理对象的内部资源。不管它的内容如何,如果一个对象在动态内存中,那么当它被调用
delete时它就会被销毁,如果一个对象在自动内存中,那么当它超出范围时它就会被销毁。如果一个对象是临时对象,它会在创建它的语句结束时超出范围。所以,如果你没有正确地实现Rule of 3/5/0,那么当copy-from/moved-from 对象被销毁时,你就会有不好的事情发生。 -
@AsteroidsWithWings 实际上,如果
A实现了复制构造函数和移动构造函数,则该运算符既可以作为复制赋值运算符,也可以作为移动赋值运算符
标签: c++ callback destructor