拷贝构造函数问题
#include<iostream>using namespace std;
class Student
{
public:
Student();
Student(const char*);
Student(Student&);
char* read() {return _name;}
~Student();
private:
char* _name;
};
Student::Student():_name(0)
{
cout<<"Student Defult Constructing called..."<<endl;
}
Student::Student(const char* name)
{
cout<<"Student Constructing called..."<<name<<endl;
_name = new char[strlen(name) + 1];
strcpy(_name,name);
}
Student::Student(Student& p)
{
cout<<"Student Copying Constrcting called... :"<<p._name<<endl;
_name = new char[strlen(p._name) + 1];
strcpy(_name,p._name);
}
Student::~Student()
{
cout<<"Student Destructing called...: "<<_name<<endl;
delete _name;
}
int main()
{
Student& a = Student("abc");
cout<<a.read()<<endl;
Student b = Student("xyz"); //为什么调用的不是拷贝构造函数呢?
cout<<b.read()<<endl;
Student c(Student("lmn"));//同上;
cout<<c.read()<<endl;
Student d("qwe");
Student e(d);
cout<<e.read()<<endl;
return 0;
}