我想知道何时将新的unique_ptr分配给先前的unique_ptr,会发生什么.在以下代码中:
std::unique_ptr<my_class> test;
......
test = std::unique_ptr<my_class>(new my_class());
......
test = std::unique_ptr<my_class>(new my_class());
Run Code Online (Sandbox Code Playgroud)
我预计当我第二次分配一个新的std :: unique_ptr进行测试时,首先调用my_class的析构函数,然后调用它的构造函数.我的预期是错的吗? 但是当我调试我的代码时,我看到的是反向的.my_class的第一个构造函数调用然后调用它的析构函数.
我有两个函数没有任何实现。
我希望链接器返回对 hello 和 world 错误的未定义引用。
但令人惊讶的是,代码编译并运行时没有任何错误。
#include <stdio.h>
int hello();
char world();
int main() {
printf("sizeof hello = %zu, sizeof world = %zu\n", sizeof(hello()), sizeof(world()));
}
Run Code Online (Sandbox Code Playgroud)
sizeof hello = 4, sizeof world = 1
Run Code Online (Sandbox Code Playgroud)