[英]Class assignment operator and copy constructor
Trying to understand how to make assignment class operator. 试图了解如何使分配类运算符。 Member variable
constant_name
is used to identify object. 成员变量
constant_name
用于标识对象。 Member variable changeable_name
is used to get affect during assignment. 成员变量
changeable_name
用于在分配期间生效。 I need this to understand who effects who and what is result of that. 我需要这样做,以了解谁影响了谁以及结果如何。 Each object gets unique
constant_name
during creation. 每个对象在创建期间都会获得唯一的
constant_name
。
class turbo
{
static string m;
public:
string changable_name ="";
string constant_name ="";
void nm()
{
m=m+"A";
changable_name=m;
constant_name=changable_name;
};
void printID()
{
printf("constant_name=%s changable_name=%s ",constant_name.c_str(),changable_name.c_str());
};
turbo() {
nm();
printID();
printf("default constructor \n");
};
turbo & operator = (turbo & value){ printID(); printf("= operator\n"); if (!(&value==this)) { changable_name = value.constant_name; } ; return *this; }
turbo (turbo&) {nm(); printID(); printf("copy constructor\n");}
};
string turbo::m;
int main( int argc, char ** argv )
{
turbo f;
turbo ff;
f=ff;
printf("--- result ---\n");
f.printID();
}
When =
operator is described in code at the top I have output: 当顶部的代码中描述了
=
运算符时,我将输出:
constant_name=A changable_name=A default constructor
constant_name=AA changable_name=AA default constructor
constant_name=A changable_name=A = operator
--- result ---
constant_name=A changable_name=AA
Field changable_name
was coppyed from AA
to A
and that is fine. 现场
changable_name
从coppyed AA
到A
,那就是罚款。
Now if I remove reference sign in turbo value
: 现在,如果我删除
turbo value
中的参考符号:
turbo & operator = (turbo value){ printID(); printf("= operator\n"); if (!(&value==this)) { changable_name = value.constant_name; } ; return *this; }
I have following in output: 我在输出中有以下内容:
constant_name=A changable_name=A default constructor
constant_name=AA changable_name=AA default constructor
constant_name=AAA changable_name=AAA copy constructor
constant_name=A changable_name=A = operator
--- result ---
constant_name=A changable_name=AAA
New object AAA
was created and assigned to object A
. 创建了新对象
AAA
并将其分配给对象A
AA
had no influence at all. AA
根本没有任何影响。 Why compiler decides to activate copy constructor and then assignment operator? 为什么编译器决定先激活复制构造函数,然后再激活赋值运算符? I suppose Copy constructor was activated in right hand side of "=" operator.
我想在“ =”运算符的右侧激活了复制构造函数。
Another case. 另一种情况。
Now if I remove reference sign in turbo operator
: 现在,如果我删除
turbo operator
中的参考符号:
turbo operator = (turbo & value){ printID(); printf("= operator\n"); if (!(&value==this)) { changable_name = value.constant_name; } ; return *this; }
I have following in output: 我在输出中有以下内容:
constant_name=A changable_name=A default constructor
constant_name=AA changable_name=AA default constructor
constant_name=A changable_name=A = operator
constant_name=AAA changable_name=AAA copy constructor
--- result ---
constant_name=A changable_name=AA
That completely confused me. 那完全让我感到困惑。 I suppose
AAA
was created in left hand side of =
. 我想
AAA
是在=
左侧创建的。 AAA has done no action at all in assignment. AAA根本没有采取任何行动。 In result
A
was assigned to AA
. 结果
A
被分配给AA
。 But why it was needed to create AAA? 但是为什么需要创建AAA?
When you don't pass the argument by reference in the copy-assignment operator, then the compiler copies the argument you pass, and the function uses that copy. 如果您没有在copy-assignment运算符中通过引用传递参数,则编译器将复制您传递的参数,然后函数使用该副本。 That the function called is the copy-assignment operator doesn't matter, it's the same for all functions.
所调用的函数是复制分配运算符并不重要,所有函数都相同。
Search and read more about pass-by-value and pass-by-reference. 搜索和阅读有关值传递和引用传递的更多信息。
As for the return value, the same thing happens there: The compiler creates a copy of the returned value. 至于返回值,这里也会发生同样的事情:编译器创建返回值的副本。 If you had properly implemented your copy-constructor to actually copy something, the result would have been different.
如果您正确实现了复制构造函数以实际复制某些内容,则结果将有所不同。
当value
参数按值而不是按引用传递时,对&value == this
的测试毫无意义,因为在调用成员函数之前在堆栈上创建了一个新的turbo
实例,并且该实例的地址显然没有等于调用对象的地址。
If you don't define below functions in your class then compiler will provide its own versions 如果您未在类中定义以下函数,则编译器将提供其自己的版本
default constructor 默认构造函数
copy constructor 复制构造函数
assignment operator overload 赋值运算符重载
destructor 析构函数
Now in your second case you have removed the reference from the argument so its a case of pass by value and it was achieved by invoking copy-constructor. 现在,在第二种情况下,您已从参数中删除了引用,因此它是按值传递的情况,这是通过调用copy-constructor实现的。
And in third case it is return by value instead of by reference, so the copy ctor again gets called after the job of = operator is done and hence the output. 在第三种情况下,它是按值而不是引用返回的,因此在完成=运算符的工作并因此输出之后,将再次调用复制ctor。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.