#include<iostream>
#include<string>
using namespace std;
class CObj{
private:
string a;
public:
CObj(string aa): a(aa)
{
cout<<"Constructor for "<<aa<<endl;
}
CObj(const CObj& cobj)
{
a = "copy";
cout<<"copy constructor for "<<a<<endl;
}
~CObj()
{
cout<<"Destructor for "<<a<<endl;
}
};
CObj global("global");
CObj func(CObj& arg)
{
CObj local("local");
static CObj st("static");
return local;
}
int main()
{
CObj a("a");
//copy constructor
CObj b = a;
//assignment
CObj c = func(a);
return 0;
}
输出:
Constructor for global
Constructor for a
copy constructor for copy
Constructor for local
Constructor for static
Destructor for local
Destructor for copy
Destructor for a
Destructor for static
Destructor for global
Program ended with exit code: 0