强制类型的转换
class CRectangle {
public:
CRectangle(CRectangle &to) {
cout << "***" << endl;
w = to.w; h = to.h;
printTotal();
}
CRectangle(int i = 1, int j = 2);
~CRectangle();
static void printTotal();
void print() { cout << w << "--" << h << endl; }
protected:
private:
int w, h;
static int nTotalArea;
static int nTotalNumber;
};
CRectangle::CRectangle(int i, int j) {
cout << "---" << endl;
w = i; h = j;
nTotalNumber++;
nTotalArea += w*h; printTotal();
}
CRectangle::~CRectangle() {
nTotalNumber--;
nTotalArea = nTotalArea - w*h;
}
int CRectangle::nTotalArea = 0;
int CRectangle::nTotalNumber = 0;
void CRectangle::printTotal() {
cout << nTotalNumber << "," << nTotalArea << endl;
}
int main() {
CRectangle c1(3, 3), c3;
CRectangle::printTotal();
c3 = 40;
c3.print();
c1.printTotal();
return 0;
}
c3 = 40; 这边其实会生成临时对象,调用普通的构造函数,不是拷贝构造函数,只是临时对象很快消亡,我是在构造函数里面“动了手脚”才最终看得到生成新对象的。
int main() {
CRectangle c1(3, 3), c3=40;
CRectangle::printTotal();
c3.print();
c1.printTotal();
return 0;
}
此时不会调用构造函数
参数是对象,而不是引用!!!
class CRectangle {
public:
CRectangle(CRectangle &to);
CRectangle(int i = 1, int j = 2);
~CRectangle();
static void printTotal();
void change(CRectangle obj) {
obj.w *= 2; printTotal();
}
void print() { cout << w << "--" << h << endl; }
protected:
private:
int w, h;
static int nTotalArea;
static int nTotalNumber;
};
CRectangle::CRectangle(CRectangle &to){
cout << "拷贝构造函数" << endl;
w = to.w; h = to.h;
printTotal();
}
CRectangle::CRectangle(int i, int j) {
cout << "构造函数" << endl;
w = i; h = j;
nTotalNumber++;
nTotalArea += w*h; printTotal();
}
CRectangle::~CRectangle() {
printTotal();
cout << "析构函数" << endl;
nTotalNumber--;
nTotalArea = nTotalArea - w*h;
printTotal();
}
int CRectangle::nTotalArea = 0;
int CRectangle::nTotalNumber = 0;
void CRectangle::printTotal() {
cout << nTotalNumber << "," << nTotalArea << endl;
}
int main() {
CRectangle c1(3, 3);
c1.change(c1);
return 0;
}
注意此时不是引用啊,此时,生成临时对象,然后拷贝构造函数里面没有及时加上1,在最后的析构函数里面就减去1。
如果改变:
void change(CRectangle &obj) {
obj.w *= 2; printTotal();
}
又或者改变拷贝构造函数
CRectangle::CRectangle(CRectangle &to){
cout << "拷贝构造函数" << endl;
w = to.w; h = to.h;
nTotalNumber++;
nTotalArea += w*h;
printTotal();
}
注意,生成的是obj的临时对象,change函数里面还自乘2,所以一下子减去18了。
返回一个对象
class CRectangle {
public:
CRectangle(CRectangle &to);
CRectangle(int i = 1, int j = 2);
~CRectangle();
static void printTotal();
void print() { cout << w << "--" << h << endl; }
protected:
public:
int w, h;
private:
static int nTotalArea;
static int nTotalNumber;
};
CRectangle::CRectangle(CRectangle &to){
cout << "拷贝构造函数" << endl;
w = to.w; h = to.h;
nTotalNumber++;
nTotalArea += w*h;
printTotal();
}
CRectangle::CRectangle(int i, int j) {
cout << "构造函数" << endl;
w = i; h = j;
nTotalNumber++;
nTotalArea += w*h; printTotal();
}
CRectangle::~CRectangle() {
printTotal();
cout << "析构函数" << endl;
nTotalNumber--;
nTotalArea = nTotalArea - w*h;
printTotal();
}
int CRectangle::nTotalArea = 0;
int CRectangle::nTotalNumber = 0;
void CRectangle::printTotal() {
cout << nTotalNumber << "," << nTotalArea << endl;
}
CRectangle experiment(CRectangle &obj) {
return obj;
}
int main() {
CRectangle c1(3, 3),c2;
c2 = experiment(c1);
c1.w = 5;
return 0;
}
这时候,在前面基础上都能够清除了,还有,析构函数总是就近原则,后生成的先析构调。
总结下调用拷贝构造函数的时候
- 参数是对象
- 返回一个对象
- 强制转换
似乎调用拷贝构造函数和临时对象生成是不分开的。