第一个场景
#include <iostream>
using namespace std;
class Test{
public:
Test(int x, int y){
cout << "-----------------------" << endl;
m_x = x;
m_y = y;
cout << "调用了有参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test() {
cout << "-----------------------" << endl;
m_x = 0;
m_y = 0;
cout << "调用了无参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test(const Test& another){
cout << "-----------------------" << endl;
m_x = another.m_x;
m_y = another.m_y;
cout << "调用了拷贝构造函数" << endl;
cout << "-----------------------" << endl;
}
void operator = (const Test& t){
cout << "-----------------------" << endl;
cout << "调用了=号操作符" << endl;
m_x = t.m_x;
m_y = t.m_y;
cout << "-----------------------" << endl;
}
void printT(){
cout << "-----------------------" << endl;
cout << "x : " << m_x << ", y : " << m_y << endl;
cout << "-----------------------" << endl;
}
~Test(){
cout << "-----------------------" << endl;
cout << "~Test()析构函数被执行了" << endl;
cout << "(" << m_x << ", " << m_y << ")" << "被析构了" << endl;
cout << "-----------------------" << endl;
}
private:
int m_x;
int m_y;
};
void test1(){
Test t1(1, 2);
Test t2(t1);
t2.printT();
}
int main(void){
test1();
}
第二个场景
#include <iostream>
using namespace std;
class Test{
public:
Test(int x, int y){
cout << "-----------------------" << endl;
m_x = x;
m_y = y;
cout << "调用了有参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test() {
cout << "-----------------------" << endl;
m_x = 0;
m_y = 0;
cout << "调用了无参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test(const Test& another){
cout << "-----------------------" << endl;
m_x = another.m_x;
m_y = another.m_y;
cout << "调用了拷贝构造函数" << endl;
cout << "-----------------------" << endl;
}
void operator = (const Test& t){
cout << "-----------------------" << endl;
cout << "调用了=号操作符" << endl;
m_x = t.m_x;
m_y = t.m_y;
cout << "-----------------------" << endl;
}
void printT(){
cout << "-----------------------" << endl;
cout << "x : " << m_x << ", y : " << m_y << endl;
cout << "-----------------------" << endl;
}
~Test(){
cout << "-----------------------" << endl;
cout << "~Test()析构函数被执行了" << endl;
cout << "(" << m_x << ", " << m_y << ")" << "被析构了" << endl;
cout << "-----------------------" << endl;
}
private:
int m_x;
int m_y;
};
void test2()
{
Test t1(1, 2);
Test t2;
t2 = t1;
}
int main(void){
test2();
}
第三个场景
#include <iostream>
using namespace std;
class Test{
public:
Test(int x, int y){
cout << "-----------------------" << endl;
m_x = x;
m_y = y;
cout << "调用了有参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test() {
cout << "-----------------------" << endl;
m_x = 0;
m_y = 0;
cout << "调用了无参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test(const Test& another){
cout << "-----------------------" << endl;
m_x = another.m_x;
m_y = another.m_y;
cout << "调用了拷贝构造函数" << endl;
cout << "-----------------------" << endl;
}
void operator = (const Test& t){
cout << "-----------------------" << endl;
cout << "调用了=号操作符" << endl;
m_x = t.m_x;
m_y = t.m_y;
cout << "-----------------------" << endl;
}
void printT(){
cout << "-----------------------" << endl;
cout << "x : " << m_x << ", y : " << m_y << endl;
cout << "-----------------------" << endl;
}
~Test(){
cout << "-----------------------" << endl;
cout << "~Test()析构函数被执行了" << endl;
cout << "(" << m_x << ", " << m_y << ")" << "被析构了" << endl;
cout << "-----------------------" << endl;
}
private:
int m_x;
int m_y;
};
void func(Test t){
cout << "func begin..." << endl;
t.printT();
cout << "func end..." << endl;
}
void test3(){
cout << "test3 begin ..." << endl;
Test t1(10, 20);
func(t1);
cout << "test3 end..." << endl;
}
int main(void){
test3();
}
第四个场景
#include <iostream>
using namespace std;
class Test{
public:
Test(int x, int y){
cout << "-----------------------" << endl;
m_x = x;
m_y = y;
cout << "调用了有参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test() {
cout << "-----------------------" << endl;
m_x = 0;
m_y = 0;
cout << "调用了无参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test(const Test& another){
cout << "-----------------------" << endl;
m_x = another.m_x;
m_y = another.m_y;
cout << "调用了拷贝构造函数" << endl;
cout << "-----------------------" << endl;
}
void operator = (const Test& t){
cout << "-----------------------" << endl;
cout << "调用了=号操作符" << endl;
m_x = t.m_x;
m_y = t.m_y;
cout << "-----------------------" << endl;
}
void printT(){
cout << "-----------------------" << endl;
cout << "x : " << m_x << ", y : " << m_y << endl;
cout << "-----------------------" << endl;
}
~Test(){
cout << "-----------------------" << endl;
cout << "~Test()析构函数被执行了" << endl;
cout << "(" << m_x << ", " << m_y << ")" << "被析构了" << endl;
cout << "-----------------------" << endl;
}
private:
int m_x;
int m_y;
};
Test func2()
{
cout << "func2 begin..." << endl;
Test temp(10, 20);
cout << "func2 end.." << endl;
return temp;
}
void test4(){
cout << "test4 begin " << endl;
func2();
cout << "test4 end" << endl;
}
int main(void){
test4();
}
第五个场景
#include <iostream>
using namespace std;
class Test{
public:
Test(int x, int y){
cout << "-----------------------" << endl;
m_x = x;
m_y = y;
cout << "调用了有参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test() {
cout << "-----------------------" << endl;
m_x = 0;
m_y = 0;
cout << "调用了无参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test(const Test& another){
cout << "-----------------------" << endl;
m_x = another.m_x;
m_y = another.m_y;
cout << "调用了拷贝构造函数" << endl;
cout << "-----------------------" << endl;
}
void operator = (const Test& t){
cout << "-----------------------" << endl;
cout << "调用了=号操作符" << endl;
m_x = t.m_x;
m_y = t.m_y;
cout << "-----------------------" << endl;
}
void printT(){
cout << "-----------------------" << endl;
cout << "x : " << m_x << ", y : " << m_y << endl;
cout << "-----------------------" << endl;
}
~Test(){
cout << "-----------------------" << endl;
cout << "~Test()析构函数被执行了" << endl;
cout << "(" << m_x << ", " << m_y << ")" << "被析构了" << endl;
cout << "-----------------------" << endl;
}
private:
int m_x;
int m_y;
};
Test func2(){
cout << "func2 begin..." << endl;
Test temp(10, 20);
cout << "func2 end.." << endl;
return temp;
}
void test5(){
cout << "test5 begin ..." << endl;
Test t1 = func2();
cout << "test5 end..." << endl;
}
int main(void){
test5();
}
第六个场景
#include <iostream>
using namespace std;
class Test{
public:
Test(int x, int y){
cout << "-----------------------" << endl;
m_x = x;
m_y = y;
cout << "调用了有参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test() {
cout << "-----------------------" << endl;
m_x = 0;
m_y = 0;
cout << "调用了无参数的构造函数" << endl;
cout << "-----------------------" << endl;
}
Test(const Test& another){
cout << "-----------------------" << endl;
m_x = another.m_x;
m_y = another.m_y;
cout << "调用了拷贝构造函数" << endl;
cout << "-----------------------" << endl;
}
void operator = (const Test& t){
cout << "-----------------------" << endl;
cout << "调用了=号操作符" << endl;
m_x = t.m_x;
m_y = t.m_y;
cout << "-----------------------" << endl;
}
void printT(){
cout << "-----------------------" << endl;
cout << "x : " << m_x << ", y : " << m_y << endl;
cout << "-----------------------" << endl;
}
~Test(){
cout << "-----------------------" << endl;
cout << "~Test()析构函数被执行了" << endl;
cout << "(" << m_x << ", " << m_y << ")" << "被析构了" << endl;
cout << "-----------------------" << endl;
}
private:
int m_x;
int m_y;
};
Test func2(){
cout << "func2 begin..." << endl;
Test temp(10, 20);
cout << "func2 end.." << endl;
return temp;
}
void test6(){
cout << "test6 begin..." << endl;
Test t1;
t1 = func2();
cout << "test6 end..." << endl;
}
int main(void){
test6();
}