Ste*_*ail 4 c++ copy-assignment
我试图了解副本分配构造函数在c ++中的工作方式。我只使用过Java,所以我真的不在这里。我已经阅读并看到返回引用是一种很好的做法,但是我不知道该怎么做。我写了这个小程序来测试这个概念:
main.cpp:
#include <iostream>
#include "test.h"
using namespace std;
int main() {
Test t1,t2;
t1.setAge(10);
t1.setId('a');
t2.setAge(20);
t2.setId('b');
cout << "T2 (before) : " << t2.getAge() << t2.getID() << "\n";
t2 = t1; // calls assignment operator, same as t2.operator=(t1)
cout << "T2 (assignment operator called) : " << t2.getAge() << t2.getID() << "\n";
Test t3 = t1; // copy constr, same as Test t3(t1)
cout << "T3 (copy constructor using T1) : " << t3.getAge() << t3.getID() << "\n";
return 1;
}
Run Code Online (Sandbox Code Playgroud)
test.h:
class Test {
int age;
char id;
public:
Test(){};
Test(const Test& t); // copy
Test& operator=(const Test& obj); // copy assign
~Test();
void setAge(int a);
void setId(char i);
int getAge() const {return age;};
char getID() const {return id;};
};
Run Code Online (Sandbox Code Playgroud)
test.cpp:
#include "test.h"
void Test::setAge(int a) {
age = a;
}
void Test::setId(char i) {
id = i;
}
Test::Test(const Test& t) {
age = t.getAge();
id = t.getID();
}
Test& Test::operator=(const Test& t) {
}
Test::~Test() {};
Run Code Online (Sandbox Code Playgroud)
我似乎不明白我应该放什么东西operator=()
。我见过有人回来了*this
,但从我读到的内容仅是对象本身的引用(在的左侧=
),对吗?然后,我想到了返回const Test& t
对象的副本,但是使用该构造函数没有意义了吗?我要返回什么,为什么?
我已经阅读并看到返回引用是一种很好的做法,但是我不知道该怎么做。
加
return *this;
Run Code Online (Sandbox Code Playgroud)
as the last line in the function.
Test& Test::operator=(const Test& t) {
...
return *this;
}
Run Code Online (Sandbox Code Playgroud)
As to the question of why you should return *this
, the answer is that it is idiomatic.
For fundamental types, you can use things like:
int i;
i = 10;
i = someFunction();
Run Code Online (Sandbox Code Playgroud)
You can use them in a chain operation.
int j = i = someFunction();
Run Code Online (Sandbox Code Playgroud)
You can use them in a conditional.
if ( (i = someFunction()) != 0 ) { /* Do something */ }
Run Code Online (Sandbox Code Playgroud)
You can use them in a function call.
foo((i = someFunction());
Run Code Online (Sandbox Code Playgroud)
They work because i = ...
evaluates to a reference to i
. It's idiomatic to keep that semantic even for user defined types. You should be able to use:
Test a;
Test b;
b = a = someFunctionThatReturnsTest();
if ( (a = omeFunctionThatReturnsTest()).getAge() > 20 ) { /* Do something */ }
Run Code Online (Sandbox Code Playgroud)
More importantly, you should avoid writing a destructor, a copy constructor, and a copy assignment operator for the posted class. The compiler created implementations will be sufficient for Test
.
归档时间: |
|
查看次数: |
113 次 |
最近记录: |