临时对象的生成

强制类型的转换

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;
}

这时候,在前面基础上都能够清除了,还有,析构函数总是就近原则,后生成的先析构调。
这里写图片描述

总结下调用拷贝构造函数的时候

  1. 参数是对象
  2. 返回一个对象
  3. 强制转换

似乎调用拷贝构造函数和临时对象生成是不分开的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值