【C++】对象使用背后调用了哪些方法
#include <bits/stdc++.h>
using namespace std;
class Test{
public:
Test(int a = 10) :ma(a){
cout << "Test(int a = 10)" << endl;
}
~Test(){
cout << " ~Test()" << endl;
}
Test(const Test &t):ma(t.ma){
cout << "Test(const Test &t)" << endl;
}
Test& operator=(const Test &t){
cout << "operator=" << endl;
ma = t.ma;
return *this;
}
private:
int ma;
};
int main(){
Test t1; // 无参构造
Test t2(t1); // 拷贝构造
// 注意,在等号左边是对象不存在而定义时,调用的是拷贝构造而不是赋值运算符重载函数。构造优于赋值
Test t3 = t1;
// Test(20) 显示生成临时对象 无名 生存周期:所在的语句
/*
* 相当于RVO的一种,用临时对象生成新对象时候,
* 临时对象被优化掉,直接产生新对象
*/
Test t4 = Test(20); // Test t4(20)
/*
* 有类名称,显示生成临时对象
*/
t4 = Test(30);
t4 = (Test)40; // int -> Test(int) 调用相应类型的构造
// 无类名称,隐式生成临时对象
t4 = 40; // int -> Test(int) 调用相应类型的构造
cout << "----------------" << endl;
// Test *p = &Test(40);
// p 指向的是一个即将析构的右值临时对象,可能导致野指针
Test &&ref = Test(50);
// 引用加长了临时对象的生存周期,生命周期变为引用变量的生命周期。可以在当前作用域内引用,作用域不再是当前语句
cout << "----------------" << endl;
return 0;
}
#include <iostream>
using namespace std;
class Test{
public:
Test(int a = 5,int b = 5)
:ma(a),mb(b)
{
cout << "Test(int, int)" << endl;
}
~Test(){
cout << "~Test()" << endl;
}
Test(const Test& t) : ma(t.ma),mb(t.mb){
cout << "Test(const Test&)" << endl;
}
Test& operator=(const Test& t){
ma = t.ma;
mb = t.mb;
cout << "Test& operator=(const Test&)" << endl;
}
private:
int ma;
int mb;
};
Test t1(10,10); // 1.
int main(){
Test t2(20,20); // 3.Test(const Test& t)
Test t3 = t2; // 4.
static Test t4 = Test(30,30); // 内存被提前分配,但是运行时才初始化 5. 构造新对象,临时对象被优化 Test(int, int)
t2 = Test(40,40); // 显示生成临时对象,赋值函数,析构临时对象
// 逗号表达式 (10,10) = (Test)50; Test(int)
t2 = (Test)(10,10);
t2 = 60; // 隐式生成临时对象
Test *p1 = new Test(30,30); // Test(int, int)
Test *p2 = new Test[2]; // Test(int, int) Test(int, int)
// Test *p3 = &Test(40,40); // 编译器识别到悬垂指针,报错
const Test &p4 = Test(30,30); // 生存周期作用域同引用
delete p1; // ~Test() free
delete []p2; // ~Test() ~Test() free
}
// 出作用域,栈上对象析构
Test t5(100,100); // 2.
函数按值传递实参到形参时,形参使用的是拷贝构造从实参拷贝到函数栈帧上
#include <iostream>
using namespace std;
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;
}
Test& operator=(const Test &t){
ma = t.ma;
cout<<"Test& operator=(const Test &t)"<<endl;
}
int getData() const{
return ma;
}
private:
int ma;
};
Test GetObject(Test t){ // Test(const Test &t)
int val = t.getData();
Test tmp(val);
return tmp; // 不要返回局部的或临时对象的指针
}
int main(){
Test t1;
Test t2;
/*
* 函数调用, 实参=>形参 是初始化 还是赋值呢
* 1. 实参=>形参 初始拷贝构造
*
*/
t2 = GetObject(t1);
}
#include <iostream>
using namespace std;
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;
}
Test& operator=(const Test &t){
ma = t.ma;
cout<<"Test& operator=(const Test &t)"<<endl;
}
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; // 不要返回局部的或临时对象的指针
/*
* 按值返回对象,调用拷贝构造临时对象到上级函数栈帧上
* 5. Test(const Test &t)
*/
/*
* 6. 析构形参 t ~Test()
* 7. 析构 tmp ~Test()
*/
}
int main(){
Test t1; // 1. Test(int)
Test t2; // 2. Test(int)
/*
* 函数调用, 实参=>形参 是初始化 还是赋值呢
* 实参=>形参 初始拷贝构造
*/
t2 = GetObject(t1);
/* 8. operator=
* 9. 析构匿名函数返回值临时对象
*/
// 10. t1 ~Test()
// 11. t2 ~Test()
}

浙公网安备 33010602011771号