How to call operator= or destructor of superclass?

I have this A class with a heap member ‘name’:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class A {
protected:
char *name;
public:
// Constructor
A() {
name = new char[10];
strcpy(name, "Undefined");
}
// Assignment Operator
virtual A &operator=(const A &rhs) {
if (this == &rhs) {
return *this;
}
delete [] name;
name = new char[10];
strcpy(name, rhs.name);
return *this;
}
// Destructor
virtual ~A() {
delete [] name;
}
};
</code>
<code>class A { protected: char *name; public: // Constructor A() { name = new char[10]; strcpy(name, "Undefined"); } // Assignment Operator virtual A &operator=(const A &rhs) { if (this == &rhs) { return *this; } delete [] name; name = new char[10]; strcpy(name, rhs.name); return *this; } // Destructor virtual ~A() { delete [] name; } }; </code>
class A {
protected:
    char *name;

public:
    // Constructor
    A() {
        name = new char[10];
        strcpy(name, "Undefined");
    }

    // Assignment Operator
    virtual A &operator=(const A &rhs) {
        if (this == &rhs) {
            return *this;
        }

        delete [] name;
        name = new char[10];
        strcpy(name, rhs.name);

        return *this;
    }

    // Destructor
    virtual ~A() {
        delete [] name;
    }
};

I have the subclass B with additional variable ‘b’:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class B : public A {
int b;
// Constructor
B():A(),b(0){}
// Assignment Operator
virtual B& operator=(const B& rhs) {
if (this == &rhs) {
return *this;
}
delete [] name;
name = new char[10];
strcpy(name, rhs.name);
b = rhs.b;
return *this;
}
// Destructor
virtual ~B() {
delete [] name;
}
};
</code>
<code>class B : public A { int b; // Constructor B():A(),b(0){} // Assignment Operator virtual B& operator=(const B& rhs) { if (this == &rhs) { return *this; } delete [] name; name = new char[10]; strcpy(name, rhs.name); b = rhs.b; return *this; } // Destructor virtual ~B() { delete [] name; } }; </code>
class B : public A {
    int b;

    // Constructor
    B():A(),b(0){}

    // Assignment Operator
    virtual B& operator=(const B& rhs) {
        if (this == &rhs) {
            return *this;
        }

        delete [] name;
        name = new char[10];
        strcpy(name, rhs.name);

        b = rhs.b;

        return *this;
    }

    // Destructor
    virtual ~B() {
        delete [] name;
    }
};

I have the subclass C with additional member ‘char* name’:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class C : public A {
int* c;
// Constructor
C():A() {
c = new int[10];
for(int i = 0 ; i < 10 ; i++) {
c[i] = 0;
}
}
// Assignment Operator
virtual C& operator=(const C& rhs) {
if (this == &rhs) {
return *this;
}
delete [] name;
name = new char[10];
strcpy(name, rhs.name);
delete [] c;
c = new int[10];
for (int i = 0 ; i < 10 ; i++) {
c[i] = rhs.c[i];
}
return *this;
}
// Destructor
virtual ~C() {
delete [] name;
delete [] c;
}
};
</code>
<code>class C : public A { int* c; // Constructor C():A() { c = new int[10]; for(int i = 0 ; i < 10 ; i++) { c[i] = 0; } } // Assignment Operator virtual C& operator=(const C& rhs) { if (this == &rhs) { return *this; } delete [] name; name = new char[10]; strcpy(name, rhs.name); delete [] c; c = new int[10]; for (int i = 0 ; i < 10 ; i++) { c[i] = rhs.c[i]; } return *this; } // Destructor virtual ~C() { delete [] name; delete [] c; } }; </code>
class C : public A {
    int* c;

    // Constructor
    C():A() {
        c = new int[10];
        for(int i = 0 ; i < 10 ; i++) {
            c[i] = 0;
        }
    }

    // Assignment Operator
    virtual C& operator=(const C& rhs) {
        if (this == &rhs) {
            return *this;
        }

        delete [] name;
        name = new char[10];
        strcpy(name, rhs.name);

        delete [] c;
        c = new int[10];
        for (int i = 0 ; i < 10 ; i++) {
            c[i] = rhs.c[i];
        }

        return *this;
    }

    // Destructor
    virtual ~C() {
        delete [] name;
        delete [] c;
    }
};

I wonder if this is the correct way to implement operator= and destructors for B and C.
Is there any way to call A’s operator= or destructor in B or C, so I don’t write assignment for all members again and again.

  • A is base class with a heap variable
  • B is a class derived from A, with additional ‘int b’
  • C is a class derived from A, with additional ‘char* name’

11

To respond to the question as asked, yes, a derived class operator=() can call a base class operator=(). For example;

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> B& operator=(const B& rhs) {
A::operator=(rhs);
b = rhs.b;
return *this;
}
</code>
<code> B& operator=(const B& rhs) { A::operator=(rhs); b = rhs.b; return *this; } </code>
    B& operator=(const B& rhs) {
       A::operator=(rhs);
       b = rhs.b;
       return *this;  
    }

(and similarly for C).

However, this isn’t a particularly good approach. In the following, I’m going to address some concerns with your approach that you did NOT ask about.

  1. It is not necessary that any of your operator=() be virtual.

  2. It would be better to not declare or define B::operator=() at all. Doing that allowed B::operator=() to be implicitly generated (e.g. by your compiler) and the implicitly generated version calls the base class version, and copy/assigns all member by value. The only reason to roll your own is if the implicitly-generated operator=() is not suitable for your derived class.

Additionally, it is not necessary to do the if (this == &rhs) in A::operator=(). Instead, it can be defined as

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>A &operator=(const A &rhs) {
char *newname = new char [std::strlen(rhs.name)];
strcpy(newname, rhs.name);
delete [] name;
name = newname;
return *this;
}
</code>
<code>A &operator=(const A &rhs) { char *newname = new char [std::strlen(rhs.name)]; strcpy(newname, rhs.name); delete [] name; name = newname; return *this; } </code>
A &operator=(const A &rhs) {
    char *newname = new char [std::strlen(rhs.name)];
    strcpy(newname, rhs.name);
    delete [] name;
    name = newname;
    return *this;
}

Apart from avoiding the this == &rhs test (which will produce different behaviour if A has an operator&()), this approach is also except safe – if the new expression throws, the object is unchanged.

The only downside of this is that, temporarily, there is a minor additional memory usage (name, rhs.name, and newname all consume memory, before name is released and reassigned).

Using this approach, C::operator=() would be implemented to call A::operator=() and to allocate/copy/release c.

Furthermore, the destructors of B and C should generally not release As members. When B and C are destructed, As constructor is called implicitly (so your approach will result in A::name being destroyed twice, which results in undefined behaviour.

Lastly, you can use standard containers (std::string, or std::vector<char>, and avoid the need for manual memory allocation (new and delete) entirely. Look up “rule of zero C++” for more information.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật