我在成长过程中经历过的结构是恶魔,因为它们往往很大,所以指针总是要走的路。既然C++ 11具有相当好的RVO(右值优化),我想知道下面的代码是否有效。你可以看到,我的类有一堆向量结构(不是指向它们的指针)。构造函数接受值结构并将其存储。用C++ 11中的RVO优化此风格的代码吗?
我-hope-是编译器将使用移动语义,以便确实没有数据复制正在进行;构造函数会(如果可能)只承担传递的值的所有权。
有谁知道,如果这是真的,偏偏自动的,或者我需要用& &语法等方面的举动构造?
// ParticleVertex
//
// Class that represents the particle vertices
class ParticleVertex : public Vertex
{
public:
D3DXVECTOR4 _vertexPosition;
D3DXVECTOR2 _vertexTextureCoordinate;
D3DXVECTOR3 _vertexDirection;
D3DXVECTOR3 _vertexColorMultipler;
ParticleVertex(D3DXVECTOR4 vertexPosition,
D3DXVECTOR2 vertexTextureCoordinate,
D3DXVECTOR3 vertexDirection,
D3DXVECTOR3 vertexColorMultipler)
{
_vertexPosition = vertexPosition;
_vertexTextureCoordinate = vertexTextureCoordinate;
_vertexDirection = vertexDirection;
_vertexColorMultipler = vertexColorMultipler;
}
virtual const D3DVERTEXELEMENT9 * GetVertexDeclaration() const
{
return particleVertexDeclarations;
}
};
请在构造函数中使用初始化列表,而不是其正文中的分配。 – Manu343726
RVO代表返回值优化。 – Rapptz
'D3DXVECTOR4'接口和实现问题,以确定如何通过它。在这种情况下,'D3DXVECTOR4'是一个4'float'的POD'struct'。 'std :: move'和'&&'对它的处理方式几乎没有影响。 – Yakk