01 对象应用优化
class Test
{
public:
// Test() Test(10) Test(10,10)
Test(int a = 5, int b = 5)
:ma(a), mb(b)
{
cout << "Test(int,int)" << endl;
}
~Test()
{
cout << "~Test()" << endl;
}
Test(const Test& src) :ma(src.ma), mb(src.mb)
{
cout << "Test(const Test& src)" << endl;
}
void operator=(const Test& src)
{
ma = src.ma;
mb = src.mb;
cout << "operator=" << endl;
}
private:
int ma;
int mb;
};
对象使用过程中背后调用的方法
int main()
{
Test t1;
Test t2(t1);
Test t3 = t1; // 调用拷贝构造,有新对象产生
/*
Test(20) 显示生产临时对象 生存周期:所在语句
C++编译器对于对象构造的优化:用临时对象生成新对象的时候,临时对象就不产生了,直接构造新对象就可以了。
*/
Test t4 = Test(20); // 与 Test t4(20); 没有区别
cout << "--------------------" << endl;
t4 = t2; // 调用赋值运算符重载函数,并无新对象产生
// t4.operator=(const Test& t)
// 以下两个都是显示生成临时对象
t4 = Test(30);
t4 = (Test)30; // int->Test(int)
// 隐式生成临时对象
t4 = 30; // Test(30) int->Test(int)
cout << "--------------------" << endl;
// 出了语句,指针p指向的将是一个已经析构的临时对象(Test(40)析构)
Test *p = &Test(40);
// 引用未析构,因引用起别名
const Test &ref = Test(50);
cout << "--------------------" << endl;
return 0;
}
Test对象调用方法的类型与顺序
Test t1(10, 10); // 1.Test(int,int)
int main() {
Test t2(20, 20); // 3.Test(int,int);
Test t3 = t2; // 4.Test(const Test& src)
// static Test t4(30,30)
static Test t4 = Test(30, 30); // 5.Test(int,int);
t2 = Test(40, 40); // 6.Test(int,int); operator= ~Test()
t2 = (Test)(50, 50); // 7.Test(int,int); operator= ~Test()
t2 = 60; // 8.Test(int,int); operator= ~Test()
Test* p1 = new Test(70, 70); // 9.Test(int,int); 出了语句不析构,只有delete时析构
Test* p2 = new Test[2]; // 10.调用两次Test(int,int); Test(int,int);
Test* p3 = &Test(80, 80); // 11.Test(int,int); ~Test()
const Test& p4 = Test(90, 90); // 12.Test(int,int);
delete p1; // 13.~Test()
delete p2; // 14.调用两次~Test() ~Test()
return 0;
}
Test t5(100, 100); // 2.Test(int,int);
02 函数调用过程中对象调用的方法
class Test
{
public:
Test(int data = 10) :ma(data)
{
cout << "Test(int)" << endl;
}
~Test() { cout << "~Test()" << 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) // 3.Test(const Test& t)
{
int val = t.getData();
Test tmp(val); // 4.Test(int)
return tmp; // 构造临时对象,拷贝构造赋值
}
int main()
{
Test t1;
Test t2;
t2 = GetObject(t1);
return 0;
}
/*
调用的方法和顺序
1.Test(int)
2.Test(int)
函数调用,实参到形参,是初始化,调用拷贝构造
3.Test(const Test& t)
4.Test(int)
5.Test(const Test& t)
6.~Test() tmp析构
7.~Test() 形参t析构
8.operator=
9.~Test() 临时对象析构
10.~Test() t1析构
11.~Test() t2析构
*/
03 三条对象优化原则
class Test
{
public:
Test(int data = 10) :ma(data)
{
cout << "Test(int)" << endl;
}
~Test() { cout << "~Test()" << 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;*/
//返回临时对象
return Test(val); //3.直接构造t2
}
int main()
{
Test t1;
Test t2 = GetObject(t1);
//t2 = GetObject(t1);
return 0;
}
04 右值引用
左值:有内存、有名字;
右值(常量、数字、临时量):没名字(临时量)、没内存
int main() {
int a = 10;
int& b = a; // 左值引用
// int &&c = a; // 出现错误:无法将左值绑定到右值引用
const int& c = 20; // 不能用左值引用绑定一个右值,const定义可以
/*
上述原理
int tmp = 20;
const int &c = tmp;
*/
int &&d = 20; // 可以把一个右值绑定到一个右值引用上
/*
上述原理
int tmp = 20;
int &&d = tmp;
*/
// CMyString&& e = CMyString("aaa");
int &f = d; // 一个右值引用变量,本身是一个左值
return 0;
}