#include <iostream>
using namespace std;
int g_n = 0;
class CTest
{
public:
CTest()
{
m_n = ++g_n;
cout << "CTest() " << m_n << endl;
}
virtual ~CTest()
{
cout << "~CTest() " << m_n << endl;
}
CTest(CTest & other)
{
m_n = ++g_n;
cout << "CTest(CTest & other) " << m_n << endl;
}
CTest & operator=(CTest & other)
{
cout << "CTest & operator=(CTest & other) " << m_n << endl;
return *this;
}
private:
int m_n;
};
CTest g_o;
void fun(CTest other)
{
}
void fun2(CTest & other)
{
}
CTest fun3()
{
CTest a;
return a;
}
CTest & fun4()
{
return g_o;
}
void main()
{
cout << "---------1-----------" << endl;
{
CTest a;
cout << "-1-" << endl;
}
cout << "---------2-----------" << endl;
{
CTest a;
CTest b = a;
cout << "-2-" << endl;
}
cout << "---------3-----------" << endl;
{
CTest a,b;
a=b;
cout << "-3-" << endl;
}
cout << "---------4-----------" << endl;
{
CTest a;
fun(a);
cout << "-4-" << endl;
}
cout << "---------5-----------" << endl;
{
CTest a;
fun2(a);
cout << "-5-" << endl;
}
cout << "---------6-----------" << endl;
{
fun3();
cout << "-6-" << endl;
}
cout << "---------7-----------" << endl;
{
CTest a = fun3();
cout << "-7-" << endl;
}
cout << "---------8-----------" << endl;
{
CTest & a = fun3();
cout << "-8-" << endl;
}
cout << "---------9-----------" << endl;
{
CTest a;
a = fun3();
cout << "-9-" << endl;
}
cout << "---------10-----------" << endl;
{
fun4();
cout << "-10-" << endl;
}
cout << "---------11-----------" << endl;
{
CTest a = fun4();
cout << "-11-" << endl;
}
cout << "---------12-----------" << endl;
{
CTest & a = fun4();
cout << "-12-" << endl;
}
cout << "---------13-----------" << endl;
{
CTest a;
a = fun4();
cout << "-13-" << endl;
}
cout << "---------14-----------" << endl;
{
fun(fun3());
cout << "-14-" << endl;
}
cout << "---------15-----------" << endl;
{
fun2(fun3());
cout << "-15-" << endl;
}
cout << "---------16-----------" << endl;
{
fun(fun4());
cout << "-16-" << endl;
}
cout << "---------17-----------" << endl;
{
fun2(fun4());
cout << "-17-" << endl;
}
cout << "---------18-----------" << endl;
}
C++变量生命周期探索代码
最新推荐文章于 2024-06-20 21:52:57 发布