Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:58:39+00:00 2026-06-15T08:58:39+00:00

I got the hint that for classes with member variables which are pointers, I

  • 0

I got the hint that for classes with member variables which are pointers, I need to pay attention to properly memory-manage them on assignment. The following methods are meant to avoid memory leaks:

class SomeClass {

SomeClass& SomeClass::operator =(SomeClass& from) {
    // clone
    if (&from != this) {
        this->dispose();
        this->clone(from);
    }
    return *this;
}

void SomeClass::clone(const SomeClass& from) {
    this->array = from.array;
}

void SomeClass::dispose() {
    delete[] this->array;
}
};

Assuming this is correct so far, is it possible to do the following:

Define a superclass Object which implements operator= and has abstract methods clone and dispose. Any class with pointer members should have Object as a superclass, so that it inherits operator= and I am reminded that I need to implement clone and dispose.

  • How would such a superclass look like?
  • Is it a good idea to do this by inheritance?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:58:40+00:00Added an answer on June 15, 2026 at 8:58 am

    No! Don’t do this.

    First, declare your operator=() as

    SomeClass& SomeClass::operator =(const SomeClass& from);
    

    Pay attention to const. In the new C++11 standard, there is another form, which allows the move semantic you have shown in your example, but then you declare the assignment operator as

    SomeClass& SomeClass::operator =(SomeClass&& from);
    

    Pay attention to &&.

    For an in depth discussion of this topic, look for rule of three or in C++11 rule of five

    Now to your question, assignment operators aren’t inherited. When you don’t define an assignment operator in your derived class, the compiler will generate a default one and hide the assignment operator of your base class. So, the assignment operator of your base class will never be seen and never be used by the clients of your derived classes.

    For more discussion about assignment operators, see for example What is the copy-and-swap idiom?.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a hint from someone today that my curl_multi() code is actually working
In another question on stackoverflow I got a hint that I can use thread
Got a seg fault from my memcpy that gdb can't give me anything else
got some problems. Built an applet that has to be used step-by-step. After each
I got an application (written in Delphi 2009) that uses an ADS Server (Version
I've got this code that works: def testTypeSpecialization: String = { class Foo[T] def
I've got some image generating code that uses UIGraphicsBeginImageContext(), UIGraphicsGetImageFromCurrentImageContext() and UIImagePNGRepresentation() to do
I want to read status information that an application provides via shared memory. I
I've got a C# service that currently runs single-instance on a PC. I'd like
I got two different lists (let's call them A and B) of lists and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.