内置类型的临时量:常量
自定义类型的临时量:变量
隐式生成的临时量:常量
类对象:临时量
形参const的作用:
1.防止实参被修改
2.接收隐式生成的临时量
类类型-》从临时量中拿出-》调构造函数初始化
对象空间可以取地址
引用可以提升临时对象的生存周期,提升到和引用的对象一样
具体代码及分析:
#include
using namespace std;
using namespace std;
class CGoods
{
public:
CGoods(char* name, int amount, float price)//带有三个参数的构造函数
{
cout << this << " CGoods(char*, int, float)" << endl;
mname = new char[strlen(name) + 1]();
memcpy(mname, name, strlen(name) + 1);
mamount = amount;
mprice = price;
}
CGoods(int amount)//带有一个参数的构造函数
{
cout << this << " CGoods(int)" << endl;
mname = new char[1];
mamount = amount;
}
CGoods()//默认构造函数
{
cout << this << " CGoods()" << endl;
mname = new char[1];
}
~CGoods()//析构函数
{
cout << this << " ~CGoods()" << endl;
delete[] mname;
mname = NULL;
}
CGoods(const CGoods& rhs)//拷贝构造函数
{
cout << this << " CGoods(const CGoods&)" << endl;
mname = new char[strlen(rhs.mname) + 1];
memcpy(mname, rhs.mname, strlen(rhs.mname) + 1);
mamount = rhs.mamount;
mprice = rhs.mprice;
}
CGoods& operator=(const CGoods& rhs)//赋值运算符重载函数
{
cout << this << " operator=(const CGoods&)" << endl;
if (this == &rhs)
{
return *this;
}
delete[] mname;
mname = new char[strlen(rhs.mname) + 1]();
memcpy(mname, rhs.mname, strlen(rhs.mname) + 1);
mamount = rhs.mamount;
mprice = rhs.mprice;
return *this;
}
private:
char* mname;
int mamount;
float mprice;
};
CGoods good1("good1", 10, 10.1);//生成了一个名叫good1的全局对象,调用带有三个参数的构造函数
int main()
{
CGoods good3("good3", 30, 30.1);//生成了一个名叫good2的全局对象,调用带有三个参数的构造函数
CGoods good4 = CGoods("good4", 40, 40.1);//临时量生成good4,生成临时对象的方式生成新对象,调用构造函数
CGoods good5(good4);//生成一个名叫good5的新对象,调用拷贝构造函数
good5 = good3;//赋值,good3赋给good5
{
public:
CGoods(char* name, int amount, float price)//带有三个参数的构造函数
{
cout << this << " CGoods(char*, int, float)" << endl;
mname = new char[strlen(name) + 1]();
memcpy(mname, name, strlen(name) + 1);
mamount = amount;
mprice = price;
}
CGoods(int amount)//带有一个参数的构造函数
{
cout << this << " CGoods(int)" << endl;
mname = new char[1];
mamount = amount;
}
CGoods()//默认构造函数
{
cout << this << " CGoods()" << endl;
mname = new char[1];
}
~CGoods()//析构函数
{
cout << this << " ~CGoods()" << endl;
delete[] mname;
mname = NULL;
}
CGoods(const CGoods& rhs)//拷贝构造函数
{
cout << this << " CGoods(const CGoods&)" << endl;
mname = new char[strlen(rhs.mname) + 1];
memcpy(mname, rhs.mname, strlen(rhs.mname) + 1);
mamount = rhs.mamount;
mprice = rhs.mprice;
}
CGoods& operator=(const CGoods& rhs)//赋值运算符重载函数
{
cout << this << " operator=(const CGoods&)" << endl;
if (this == &rhs)
{
return *this;
}
delete[] mname;
mname = new char[strlen(rhs.mname) + 1]();
memcpy(mname, rhs.mname, strlen(rhs.mname) + 1);
mamount = rhs.mamount;
mprice = rhs.mprice;
return *this;
}
private:
char* mname;
int mamount;
float mprice;
};
CGoods good1("good1", 10, 10.1);//生成了一个名叫good1的全局对象,调用带有三个参数的构造函数
int main()
{
CGoods good3("good3", 30, 30.1);//生成了一个名叫good2的全局对象,调用带有三个参数的构造函数
CGoods good4 = CGoods("good4", 40, 40.1);//临时量生成good4,生成临时对象的方式生成新对象,调用构造函数
CGoods good5(good4);//生成一个名叫good5的新对象,调用拷贝构造函数
good5 = good3;//赋值,good3赋给good5
static CGoods good12 = 10;//隐式生成临时对象,静态局部变量,在data段生成,调用带有一个整型参数的构造函数
CGoods good6 = 10;//显示生成临时对象,调用带有一个参数的构造函数,分号之后结束
good6 = 10;//调用带有一个参数的构造生成临时量,隐式生成临时对象,调用赋值运算符重载函数,然后调用析构销毁临时对象
good6 = 10;//调用带有一个参数的构造生成临时量,隐式生成临时对象,调用赋值运算符重载函数,然后调用析构销毁临时对象
CGoods good13 = (CGoods)("good13", 130, 10);//以生成临时对象的方式生成新对象,调用构造函数
CGoods* pgood7 = new CGoods("good7", 70, 70.1);//非法访问,不能指向临时对象
CGoods* pgood8 = new CGoods[2];//调用两次构造函数
CGoods* pgood8 = new CGoods[2];//调用两次构造函数
delete pgood7;//析构函数
delete[] pgood8;//两次析构函数
delete[] pgood8;//两次析构函数
CGoods& rgood9 = CGoods("good9", 90, 90.1);//显示生成临时对象,在分号之后结束,临时对象销毁,引用不生成对象,调用析构
CGoods* pgood10 = &CGoods("good10", 100, 100.1);//显示生成临时对象,赋值过程中没有新对象的生成
const CGoods& rgood11 = 10;//隐式生成临时对象,调用一个参数的构造生成临时量
//const CGoods* pgood12 = 10;//内置类型不能赋给指针类型
return 0;
}
CGoods good2("good2", 20, 20.1);//全局对象good2生成,调用带有三个参数的构造函数
CGoods* pgood10 = &CGoods("good10", 100, 100.1);//显示生成临时对象,赋值过程中没有新对象的生成
const CGoods& rgood11 = 10;//隐式生成临时对象,调用一个参数的构造生成临时量
//const CGoods* pgood12 = 10;//内置类型不能赋给指针类型
return 0;
}
CGoods good2("good2", 20, 20.1);//全局对象good2生成,调用带有三个参数的构造函数
pgood11先析构,然后是,pgood13,pgood10,good6,good5,good4,good3,good12(因为在data段,静态对象),good2,good1(他俩都是全局,生存周期长)(析构一般情况下遵循先构造的后析构,除了先前delete的新对象,还有就是要注意static定义的静态局部对象)
程序截图: