我尝试理解C++中的内存分配方式:
这是完整的代码源码:
Test other = toto();
这是完整的代码源码:
#include <iostream>
class Test
{
public:
Test()
{
j = i++;
std::cout<<"default constructor "<<j<<std::endl;
}
Test(const Test&)
{
std::cout<<"constuctor by copy "<<j<<std::endl;
}
Test & operator=(const Test&)
{
std::cout<<"operator = "<<j<<std::endl;
return *this;
}
int j;
static int i;
};
int Test::i = 0;
Test toto()
{
Test t;
return t;
}
int main()
{
Test other = toto();
std::cout<<other.j<<std::endl;
Test another;
return 0;
}
这段代码没有使用拷贝构造函数或重载运算符=,所以我真的不太明白它是如何工作的...
我使用的是gcc 4.7.0版本。
谢谢你的帮助 :)
Jerome