一、派生类不使用new
class father
{
private:
char* name;
int age;
public:
father(const char* name, int age) //构造函数
{
int len = strlen(name);
this->name = new char[len + 1];
strcpy(this->name, name);
this->age = age;
}
father(const father& f) //复制构造
{
this->age = f.age;
int len = strlen(f.name);
this->name = new char[len + 1];
strcpy(this->name, f.name);
}
father& operator=(const father& f)
{
if (this == &f)
return *this;
delete[] name;
int len = strlen(f.name);
name = new char[len + 1];
strcpy(name, f.name);
return *this;
}
virtual~father()
{
delete[] name;
}
};
基类father中,包含了使用new时需要的特殊方法:析构函数、复制构造符函数和重载赋值运算符。
现在从基类father中派生son类,son类不使用new。son中不需要显式析构函数,复制构造函数和赋值运算符,为什么呢?
class son :public father
{
private:
int grade;
public:
son(const char* name, int age, int grade):father(name, age), grade(grade){}
};
析构函数:son中没有定义析构函数,编译器会定义一个不执行任何操作的默认析构函数。而派生类son的默认析构函数会调用基类father的析构函数,因为派生类中新增的成员不需要执行任何特殊操作,因此默认析构函数就足够了。
复制构造函数:默认复制构造函数是浅拷贝,不适用于动态内存分配,但是对于新成员grade来说是适合的,对于成员grade的复制,通过常规赋值完成即可。
赋值运算符:派生类son的默认赋值运算符会自动使用基类father中的赋值运算符来对基类中的成员进行赋值。son中的成员grade用默认赋值运算符就够,因此,默认赋值运算符也适合,没有必要显式的重载。
二、派生类使用new
class son :public father
{
private:
char* id;
public:
son(const char* name, int age, const char* s):father(name, age)
{
int len = strlen(s);
id = new char[len + 1];
strcpy(id, s);
}
son(const son& s):father(s)
{
id = new char[strlen(s.id) + 1];
strcpy(id, s.id);
}
~son()
{
delete[] id;
}
son& operator=(const son& s)
{
if (this == &s)
return *this;
father::operator=(s);
delete[] id;
id = new char[strlen(s.id) + 1];
strcpy(id, s.id);
return *this;
}
};
在这种情况下,必须为派生类定义显式析构函数,复制构造函数和赋值运算符
析构函数:派生类son的析构函数必须释放指针id管理的内存,需要显式的析构函数
复制构造函数:son的复制构造函数只能访问son的数据,因此它必须调用父类father的复制构造函数来处理父类成员。
赋值运算符:由于son类也使用动态内存分配,所以需要一个显式的赋值运算符。
1061

被折叠的 条评论
为什么被折叠?



