简体   繁体   English

分配给用C ++创建的对象

[英]Assign to object being created in c++

I'm writing some functions to operate on vector data. 我正在编写一些对矢量数据进行操作的函数。

I defined the objects non-copyable (private copy constructor and assignment operator). 我定义了不可复制的对象(私有复制构造函数和赋值运算符)。

Then I defined the templated operator = 然后我定义了模板运算符=

template <typename G>
inline const TMatrix &operator=(const G &gen) {
    ir_mat::Copy<G, Dimension>::start(m_data, gen);
    return *this;
}

and some extra operators, like '+', '*', as described in this article . 和一些额外的运算符,例如'+','*',如本文所述

Now I can assign the result of an expression to an object: 现在,我可以将表达式的结果分配给对象:

Vector3f v1, v2, v3;
v1 = v2 + v3;

Why can't I declare a variable and assign it in a single statement? 为什么我不能在一个语句中声明变量并将其赋值?

Vector3f v1, v2;
Vector3f v3 = v1 + v2;

Is it because this assignment tries to create a temporary object before instantiating the variable, and then copy it into the new object? 是因为此分配尝试在实例化变量之前创建一个临时对象,然后将其复制到新对象中? Can I use my operator '=' also for instantiating new objects, without temporary storage? 是否可以在没有临时存储的情况下使用运算符'='来实例化新对象? Do I have to define a special constructor for that? 我是否需要为此定义一个特殊的构造函数?

Update 更新资料

I also defined a templated copy constructor (in its simplest form, probably): 我还定义了一个模板化副本构造函数(可能以其最简单的形式):

template <typename G>
TMatrix(const G &data) {
    operator=(data);
}

Now I can also instantiate v3 as: 现在,我还可以将v3实例化为:

Vector3f v3(v1 + v2);

But still no luck with the other assignment: 但是其他任务仍然没有运气:

Vector3f v3 = v1 + v2;

Initialising an object of class type with the = initialisation syntax does not use operator= , it uses the copy constructor. =初始化语法初始化类类型的对象不使用operator= ,而是使用复制构造函数。 That is, Vector3f v3 = v1 + v2; 也就是说, Vector3f v3 = v1 + v2; is just the same as Vector3f v3(v1 + v2); Vector3f v3(v1 + v2); . If your copy constructor is not accessible, you will not be able to do this. 如果您的副本构造函数不可访问,则将无法执行此操作。

operator= is only ever called on an object that has already been constructed (as with v1 = v2 + v3 ), and in the form T& operator=(const T&) is known as copy assignment . 仅对已经构造的对象调用过operator= (与v1 = v2 + v3 ),其形式为T& operator=(const T&)称为副本分配

This initialization: 此初始化:

 Vector v3(v1 + v2);

will call your nice templated constructor. 将调用您的模板化构造函数。

This initialization: 此初始化:

 Vector v3 = v1 + v2;

ends up getting translated to this: 最终被翻译为:

 Vector v3(Vector(v1 + v2));

Essentially the compiler tries to use the copy constructor and in so doing attempts to figure out how to convert the initializer to the appropriate type. 本质上,编译器尝试使用复制构造函数,从而试图找出如何将初始化程序转换为适当的类型。

Choosing to create a type that's assignable to from random types, but can't be copy assigned or copy constructed is a very weird choice. 选择创建可从随机类型分配给您的类型,但是不能进行复制分配或复制构造的类型是一个非常奇怪的选择。 I see that my assumption that you're doing it to avoid temporaries is correct. 我认为您为避免临时工所做的假设是正确的。 Except you're not doing that because temporaries are expensive exactly, but because you want to be building up an expression that will be evaluated later. 除非您这样做不是因为临时性确实很昂贵,而是因为您想构建一个稍后将要求值的表达式。 Presumably because you want to vectorize complex expression evaluation. 大概是因为您要对复杂的表达式求值向量化。

Yes, you won't be able to use the assignment form of initialization then for that. 是的,您将无法使用初始化的赋值形式。 At least, I can't think of a way to make it work. 至少,我想不出一种使它起作用的方法。

My suggestion would be to somehow transform Vector into being just a handle for the expression you're building. 我的建议是以某种方式将Vector转变为您要构建的表达式的句柄。 That handle could be a handle to a real vector that's an input. 该句柄可能是作为输入的实向量的句柄。 Or it could be the handle to a VectorExpression that's the result of operating on vectors. 也可能是VectorExpression的句柄,它是对向量进行运算的结果。

If you don't want shared ownership of the objects you have handles to, you still won't be able to implement copy assignment or copy construction. 如果您不希望您拥有处理对象的共享所有权,您仍然将无法实现副本分配或副本构造。 But in C++11 you could still implement move construction and move assignment of the handle. 但是在C ++ 11中,您仍然可以实现移动构造和句柄的移动分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM