VS2019 对类类型的匿名对象的优化

#include <iostream>
using namespace std;

class Count {
public:
    Count(const int p) {
        this->p = p;
        std::cout << "第" << ++count_gou << "次" << "构造\n";
    }
    Count(const Count& ex) {
        p = ex.p;
        std::cout << "第" << ++count_copy << "次" << "拷贝构造\n";
    }
    Count& operator=(Count& ex) {
        std::cout << "第" << ++count_deng << "次" << "重载=号\n";
        return *this;
    }

    ~Count() {
        std::cout << "第" << ++count_xi << "次" << "析构函数\n";
    }

    void print() {
        std::cout << "p = " << p << "\n";
    }

public:
    int p;
    static int count_gou;
    static int count_copy;
    static int count_xi;
    static int count_deng;
};

int Count::count_gou = 0;
int Count::count_copy = 0;
int Count::count_xi = 0;
int Count::count_deng=0;


Count test01() {
    Count p1(11);
    std::cout << "最是人间留不住\n";
    return Count(13);
    //返回匿名对象
    //有人接:匿名变有名
    //无人接:析构
}

Count&& test02() {
    std::cout << "不拘一格降人才\n";
    return Count(12);
    //返回匿名对象实体
    //有人接:首先调用析构将匿名对象析构,然后调用拷贝构造
    //无人接:释放
}
Count test03() {
    Count p1(11);
    std::cout << "直挂云帆济沧海\n";
    return p1;
    //返回p1,先调用拷贝构造,生成匿名对象,析构p1,返回匿名对象
    //有人接:匿名对象变有名
    //无人接:析构
}
Count& test04() {
    Count p1(11);
    std::cout << "朱颜辞镜花辞树\n";
    return p1;
    //返回p1实体
    //有人接:首先析构p1,然后调用拷贝构造
    //无人接:析构
}


void Test01() {
    Count p4 =test01();
    test01();
    Count p5=test02();
    test02();
    Count p6 = test03();
    test03();
    Count p7 = test04();
    test04();
}


void Ttest01() {

    throw Count(110);
    //throw抛出匿名对象实际上被认为和实体一样
    //注意如果有人接,其会先被拷贝,再被析构
    //这点与return 返回的实体不太一致。

}

void Ttest02() throw(Count) {
    
    Count p1(11);
    throw  p1;
}

void TTest01() {
    try {
        Ttest01();
    }
    catch (Count e) {
        e.print();
    }
}



void TTest02() {
    try {
        Ttest01();
    }
    catch (Count e) {
        e.print();
    }
}
int main() {
    
   //TTest01();
   TTest02();
   return 0;

}
上一篇:Android 4.4源码下载与编译


下一篇:VS2019手动升级