代码如下:
class Student
{
public:
int Age;
char* Name;
Student(int age,char *name)
{
Age=age;
Name=new char[strlen(name)+1];
strcpy_s(Name,strlen(name)+1,name);
}
~Student()
{
if(Name!=NULL)
{
delete[] Name;
Name=NULL;
}
}
};
class Teacher
{
public:
int Age;
char *Name;
Teacher(int age,const char* name)
{
Age=age;
Name=new char[strlen(name)+1];
strcpy_s(Name,strlen(name)+1,name);
}
~Teacher()
{
if(Name!=NULL)
{
delete[] Name;
Name=NULL;
}
}
Teacher(Student& s)
{
Age=s.Age;
Name=new char[strlen(s.Name)+1];
strcpy_s(Name,strlen(s.Name)+1,s.Name);
cout<<"隐形转换函数被调用"<<endl;
}
operator Student()
{
cout<<"强制转换成Student"<<endl;
return Student(Age,Name);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
char name[]="baojun";
char sname[]="SSS";
Teacher T(10,name);
Student S=Student(T);
system("pause");
}
1,构造函数里有内存泄漏问题
2,Student S=Student(T);是T转换Student,哪有这个转换
Teacher(Student& s)
{
Age=s.Age;
Name=new char[strlen(s.Name)+1];
strcpy_s(Name,strlen(s.Name)+1,s.Name);
cout<<"隐形转换函数被调用"<<endl;
}
我知道内存泄露,但不知道是哪里泄露的
额 太马虎了 我本意是这样的
Student s(10,name);
Teacher T=Teacher(s);
写错了 呵呵