引用绑定和复制构造函数/移动构造函数
Reference binding and the copy constructor/move constructor
如果我有这样的函数:
int foo(std::vector<int>* integer1ArrayIn, * integer2ArrayIn) {
std::vector<int>& integer1Array = *integer1ArrayIn;
std::vector<int>& integer2Array = *integer2ArrayIn;
}
- 引用
integer1Array
调用复制构造函数/移动构造函数来复制传入参数的元素吗? - 将引用绑定到取消引用的指针是否会调用复制构造函数?
- 引用绑定在什么情况下调用复制构造函数?
- 有人可以解释一下此代码在内存中执行时会发生什么吗?
谢谢
- 不。
- 不,但如果它是空的,它会严重崩溃。考虑在参数必须存在时通过引用传递,并在参数可能存在时通过指针传递(但也始终验证 nullptr!
- 每当 l 值(在本例中为
integer1Array
和integer2Array
(是指针或引用时,它永远不会调用 copy/move 构造函数。
如果你有std::vector integer1Array = *integer1ArrayIn
它会有效地制作副本。
你可以用乔纳斯的答案来玩弄,亲眼看看:)
1(不,没有制作副本。你可以用一个小程序来测试它,就像这样。
#include <iostream>
struct foo
{
foo() { std::cout << "Constructor" << std::endl; }
foo(const foo&) { std::cout << "Copy constructor" << std::endl; }
foo& operator=(const foo&) { std::cout << "Copy assignment operator" << std::endl; }
};
int main() {
foo* A = new foo;
foo& B = *A;
delete A;
}
2(小心nullptr
!否则一切都很好。
3(从不(见AlexG的回答(
4(不确定"代码在内存中执行"是什么意思,因为代码不是在内存中执行的。如果你的意思是程序执行时内存会发生什么,那就是另一回事了
相关文章:
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- std::vector::p ush_back() 不会在 MSVC 上编译具有已删除移动构造函数的对象
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么调用复制构造函数而不是移动构造函数?
- 基类中的默认析构函数禁用子类中的移动构造函数(如果有成员)
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 具有已删除移动和复制构造函数的类的就地构造
- 移动构造函数和右值引用
- 使用移动调用对等构造函数unique_ptr默认构造函数
- 为什么 std::memmove 中联合的默认非平凡移动构造函数C++?
- 具有专用化的模板类中的可靠条件复制和移动构造函数
- 构造函数采用std::string_view与std::string并移动
- C++:为什么不调用移动构造函数?
- 了解构造函数在移动、复制、赋值语义中的行为
- 没有移动的构造函数移动课程
- 引用绑定和复制构造函数/移动构造函数
- 构造函数移动
- C++ 向量实现 - 移动构造函数 - 移动与前进
- 我真的必须取消移动构造函数/移动结构中的所有成员还是只是指针
- 将类(没有默认构造函数)移动到另一个类的move构造函数中