在共享指针的值中调用 std::swap 调用一堆构造函数和析构函数
Calling std::swap in shared pointers' values call a bunch of constructors and destructors
我最近开始学习移动语义和共享指针,我很难理解它。
我目前正在学习有关这些主题的课程,但讲师没有解释为什么当我们交换指针中包含的值时会调用这些构造函数和析构函数。在共享指针中调用交换(如在 b.swap(a))和 std::swap(a, b) 中不调用任何构造函数或析构函数,而对 std::swap(*a, *b) 的调用调用调用一堆构造函数或析构函数。
主代码:
int main(int argc, char** argv){
std::shared_ptr<strc> a = std::make_shared<strc>("one");
std::shared_ptr<strc> b = std::make_shared<strc>("two");
message("b.swap(a)"); // No constructors or destructors are called.
b.swap(a);
disp(a);
disp(b);
message("std::swap"); // A bunch of constructors and destructors are called.
std::swap(*a, *b);
disp(a);
disp(b);
message("std::swap"); // No constructor or destructors are called.
std::swap(a, b);
disp(a);
disp(b);
return 0;
}
"strc"的实现类是(为了简洁起见,我只是要展示其中的"重要"实现):
strc::strc(strc && o){
msg("Move Constructor.");
this->data = std::move(o.data);
o.data = nullptr;
}
strc::~strc(){
msg("Destructor.");
delete [] data;
}
strc & strc::operator = (strc o){
msg("Copy and Swap (=).");
swap(o);
return *this;
}
void strc::swap(strc & o){
msg("Calling std::swap");
std::swap(this->data, o.data);
}
这是正在打印到控制台的内容(让我更多地了解正在发生的事情,并对移动语义和共享指针有透彻的了解)。
调用 b.swap(a)一 (1) 二 (1)
调用 std::swap(*a, *b)strc: 移动构造函数。 strc:移动构造函数。 strc:复制和交换 (=)。 strc:调用标准::交换 strc:析构函数。 strc:移动构造函数。 strc:复制和交换 (=)。 strc:调用标准::交换 strc:析构函数。 strc:析构函数。 二 (1) 一 (1)
调用标准::交换(a, b)一 (1) 二 (1)
为什么?它与移动语义有关吗?它不应该调用标准交换函数吗?我无法理解这些交换调用之间的区别,以及为什么其中一个调用所有这些构造函数和析构函数。
b.swap(a)
交换指针。std::swap(*a, *b)
交换尖头对象的内容。std::swap(a, b)
再次交换指针。
移动构造函数在案例 2 中使用。这就是他们的目的。
移动构造函数也用于案例 1 和 3,但您没有观察到这一点,因为您没有修改std::shared_ptr
构造函数。
您可以在此处了解更多信息 复制和交换成语应该成为 C++11 中的复制和移动习语吗?
相关文章:
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么 zlib 放气初始化调用一次不起作用?
- 如何使用结构和指针推动和弹出一堆双打
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- 什么是仅调用一次并调用参数的控制台应用
- 谷歌基准测试,如何只调用一次代码?
- Android JNI RegisterNatives:对所有内容调用一次,或者每个函数调用一次
- 当我在结构中包含多个数组时,我的程序会跳过一堆代码
- 系统调用:一次和多次,哪个更好?
- 在共享指针的值中调用 std::swap 调用一堆构造函数和析构函数
- C++菱形问题 - 如何仅调用一次基方法
- 函数被多次执行,同时调用一次
- 使用带有.lib和一堆.dll和.h文件的Python CFFI
- 用 c++ 编写一堆类似的 if 语句的漂亮方法
- 当我尝试编译程序时,我遇到了一堆错误,例如:'std::max':找不到匹配的重载函数
- Swig C++ to python:编译一堆.cpp和.h文件
- template元编程:将一堆模板参数相乘
- 在.NET中存储一堆恒定值的最佳方法
- 我的一堆函数出现"undefined reference"错误,我不知道为什么
- 只为NOT STATIC类的EACH对象调用一次方法