Я читал C++ Primer Chapter 13 «Class Inheritance», что-то о том, что оператор присваивания в производном классе путает меня. Вот случай: В базовом классе:Оператор присваивания класса C++
class baseDMA
{
private:
char * label;// label will point to dynamic allocated space(use new)
int rating;
public:
baseDMA & operator=(const baseDMA & rs);
//remaining declaration...
};
//implementation
baseDMA & baseDMA::operator=(const baseDMA & rs)
{
if(this == &rs)
{
return *this;
}
delete [] label;
label = new char[std::strlen(rs.label) + 1];
std::strcpy(label,rs.label);
rating = rs.rating;
return *this;
}
// remaining implementation
В производном классе
class hasDMA : public baseDMA
{
private:
char * style;// additional pointer in derived class
public:
hasDMA & operator=(const hasDMA& rs);
//remaining declaration...
};
// implementation
hasDMA & hasDMA::operator=(const hasDMA & hs)
{
if(this == &hs)
return *this;
baseDMA::operator=(hs);
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
return *this;
}
// remaining implementation
мой вопрос: в производном классе определения оператора присваивания, поэтому не нужно удалять стиль первого (например, удалить label в baseDMA), прежде чем назначить ему новые пробелы?
спасибо.
Вам необходимо удалить его, иначе вы закончите утечку памяти. –