0

私は C++ Primer の第 13 章「クラスの継承」を読んでいましたが、派生クラスの代入演算子について何か混乱しています。これがケースです:基本クラスでは:

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

私の質問は次のとおりです。派生クラスの割り当て演算子の定義では、新しいスペースを割り当てる前に、最初にスタイルを削除する必要がないのはなぜですか(baseDMAのラベルの削除など)?

ありがとうございました。

4

1 に答える 1