#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
class A
{
public:
A()
{
printf("A的构造函数\n");
strcpy(m_buf,"hello");
}
A(const A& a)
{
printf("A的拷贝构造函数\n");
strcpy(m_buf, a.m_buf);
}
~A()
{
printf("A的析构函数\n");
}
void f()
{
printf("A的普通成员函数:%s\n",m_buf);
}
private:
char m_buf[6];
};
//绝对不可以返回局部对象的引用,返回引用时不会调用拷贝构造函数
A f1()
{
A a;
return a;
}
int main()
{
A a=f1();//无论用引用接收还是对象接收,都不应该返回局部对象的引用
a.f();
getchar();
return 0;
}
VC++环境下的运行结果:
A的构造函数
A的拷贝构造函数
A的析构函数
A的普通成员函数:hello