是什么解释了这个片段中如此多的移动和破坏
What explains so many moves and destructions in this snippet
为了理解五法则,我想出了这个:
#include <iostream>
#include <vector>
class A
{
public:
A(int y)
{
std::cout << "constructedn";
x = new int[y];
}
~A()
{
std::cout << "destructedn";
delete[] x;
}
A(const A &other)
{
std::cout << "copiedn";
if (other.x != nullptr)
x = new int[sizeof(other.x) / sizeof(other.x[0])];
}
A(A && other) :
x(other.x)
{
std::cout << "movedn";
other.x = nullptr;
}
A& operator=(A other)
{
std::cout << "assignmentsn";
std::swap(x, other.x);
return *this;
}
int *x;
};
class B
{
private:
std::vector<A> a;
public:
B()
{
for (int i = 0; i < 5; i++)
a.emplace_back(A(1));
std::cout << "----------------------n";
for (int i = 0; i < 5; i++)
a.push_back({ 2 });
}
};
int main()
{
B *b = new B();
std::cin.get();
}
相反,如果我分别使用 emplace_back 和 push_back 1 次,无论我调用每个方法的顺序如何,我都会得到以下输出:
constructed
moved
destructed
----------------------
constructed
moved
destructed
moved
destructed
我接受这一点。但是,如果我使用上面写的代码,我会得到一些棘手的模式:
constructed
moved
destructed
constructed
moved
destructed
moved
destructed
constructed
moved
moved
destructed
destructed
moved
destructed
constructed
moved
moved
moved
destructed
destructed
destructed
moved
destructed
constructed
moved
moved
moved
moved
destructed
destructed
destructed
destructed
moved
destructed
----------------------
constructed
moved
destructed
constructed
moved
moved
moved
moved
moved
moved
destructed
destructed
destructed
destructed
destructed
destructed
moved
destructed
constructed
moved
destructed
constructed
moved
destructed
constructed
moved
moved
moved
moved
moved
moved
moved
moved
moved
destructed
destructed
destructed
destructed
destructed
destructed
destructed
destructed
destructed
moved
destructed
是什么解释了这么多"移动"和"破坏"?另外,我们是否可以说emplace_back比仅通过此输出push_back更好?
我认为此输出不是错误
如果 std::vector 的大小已满并且您尝试推送,Vector 将分配更多内存并将原始内存块中的对象移动到新的内存块并销毁原始内存块。
所以 您可以使用 std::vector.reserve((
#include <iostream>
#include <vector>
class A
{
public:
A(int y)
{
std::cout << "constructedn";
x = new int[y];
}
~A()
{
std::cout << "destructedn";
delete[] x;
}
A(const A &other)
{
std::cout << "copiedn";
if (other.x != nullptr)
x = new int[sizeof(other.x) / sizeof(other.x[0])];
}
A(A && other) :
x(other.x)
{
std::cout << "movedn";
other.x = nullptr;
}
A& operator=(A other)
{
std::cout << "assignmentsn";
std::swap(x, other.x);
return *this;
}
int *x;
};
class B
{
private:
std::vector<A> a;
public:
B()
{
//reserve
a.reserve(5);
for (int i = 0; i < 5; i++)
a.emplace_back(A(1));
std::cout << "----------------------n";
// for (int i = 0; i < 5; i++)
// a.push_back({ 2 });
}
};
int main()
{
B *b = new B();
std::cin.get();
}
此输出将如下所示
constructed
moved
destructed
constructed
moved
destructed
constructed
moved
destructed
constructed
moved
destructed
constructed
moved
destructed
相关文章:
- 请解释"函数1(p1,p2,p3);"的输出
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 如何从具有移动语义的类对象中生成共享指针
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- C / C++ 移位/偏移/向左或向右移动位图?
- 被解释为低级别const的const对象的地址
- 计算每个节点的树高,帮助我解释这个代码解决方案
- MSVC将仅移动结构参数解释为指针
- 内联程序集printf将整数解释为地址
- 自定义先决条件对移动分配运算符有效吗
- 返回值优化:显式移动还是隐式
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 有人能解释一下为什么下界是这样工作的吗C++的
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 是什么解释了这个片段中如此多的移动和破坏
- 构造函数解释为函数调用,并显式跳过移动构造函数