44对象优化

对象优化

对象优化的原则

  • 函数参数传递的过程中,对象优先按引用传递,不要按值传递,这样少形参的构造和析构
  • 函数返回对象时,优先返回一个临时对象,而不要返回一个定义过的对象,这样少一个函数中对象的构造析构
  • 接受返回值是对象的函数调用时,用初始化的方式接受,而不用先定义再赋值的方式,这样实现了临时对象拷贝构造目标对象优化,少了一个返回值临时对象。
#include <iostream>

using namespace std;

class Test {
public:
    Test(int a = 5, int b = 5) : ma(a), mb(b) { cout << "Test(" << ma << ", " << mb << ")" << endl; }

    ~Test() { cout << "~Test(" << ma << ", " << mb << ")" << endl; }

    Test(const Test &t) : ma(t.ma), mb(t.mb) {
        cout << "Test(const Test& t)" << "Test(" << ma << ", " << mb << ")" << endl;
    }

    Test &operator=(const Test &t) {
        cout << "operator=" << endl;
        ma = t.ma;
        mb = t.mb;
        return *this;
    }

    int get() {
        return ma + mb;
    }


    Test &operator+(const Test &t) {
        cout << "operator+" << endl;
        ma = t.ma + ma;
        return *this;
    }

    void setMa(int ma) {
        this->ma = ma;
    }

private:
    int ma;
    int mb;
};

//不能返回临时对象的指针或者引用
Test func(Test t1, Test t2) { //实参->形参拷贝构造,如果是传引用就少了t1和t2的拷贝构造和析构
//    cout << t1.get() << endl << t2.get() << endl;
    Test t = t1 + t2;  //operator+,然后拷贝构造,这里没有临时对象
//    cout << t1.get() << endl << t2.get() << endl;
    t.setMa(20);
    return t;
    //拷贝构造临时对象保存t,析构t,t1,t2
}

Test t1(10, 10); //全局变量在main函数执行前构造,main函数结束后析构

int main() {
    Test t1;
    cout << "----------------------" << endl;
    Test t2(t1);
    cout << "----------------------" << endl;
    Test t3 = t1;
    cout << "----------------------" << endl;
    Test(20, 30);  //临时对象的构造和析构
    cout << "----------------------" << endl;
    Test t4 = Test(20, 30);  //C++编译器对对象构造的优化:用临时对象产生新对象时,是直接构造新对象,而并没有产生临时对象且拷贝构造
    cout << "----------------------" << endl;
    t4 = Test(30, 30);  //临时对象的构造,赋值运算符重载,临时对象的析构
    cout << "----------------------" << endl;
    t4 = (Test) 30;  //整数强制类型转换->class,编译器查询合适的构造函数,隐式产生一个临时对象,同时调用等号重载
    t4 = 31;
    cout << "----------------------" << endl;
//    Test* p = &Test(40);  //不安全的,部分编译器编译不过

    Test *p1 = new Test[2];
    cout << "----------------------" << endl;
    delete[] p1;
    cout << "----------------------" << endl;
    const Test &ref = Test(50); // 是ok的,临时对象不会被马上析构
    cout << "----------------------" << endl;
    static Test t5 = Test(24, 24); // 程序运行结束时析构
    cout << "----------------------" << endl;
    t5 = func(t1, t2); //函数调用时,从实参->形参,进行拷贝构造初始化,返回的临时对象会在该语句结束后析构
    cout << "----------------------" << endl;
}

Test t10(20, 30); //全局变量在main函数执行前构造,main函数结束后析构
#include <iostream>
using namespace std;

class Test
{
public:
    Test(int data = 10) : ma(data) { cout << "Test(" << ma << ")" << endl; }
    ~Test() { cout << "~Test(" << ma << ")" << endl; }
    Test(const Test& t) : ma(t.ma) { cout << "Test(const Test& t)" << endl; }
    void operator = (const Test& t)
    {
        cout << "operator =" << endl;
        ma = t.ma;
    }
    int getData() const { return ma; }
private:
    int ma;
};

//Test GetObject(Test t) // 不能返回局部的或者临时的对象的指针或引用
//{
//    int val = t.getData();
//    Test tmp(val);
//    return tmp;
//}
//
//int main()
//{
//    Test t1;
//    Test t2;
//    t2 = GetObject(t1);
//    return 0;
//}

Test opti_GetObject(Test &t) // 不能返回局部的或者临时的对象的指针或引用
{
    int val = t.getData();
    return Test(val);
}

int main()
{
    Test t1;
    Test t2 = opti_GetObject(t1);

    return 0;
}
posted @ 2024-03-04 21:45  SIo_2  阅读(32)  评论(0)    收藏  举报