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 7808301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:05:55+00:00 2026-06-02T03:05:55+00:00

I have an abstract base class that enforces certain operations on all derived classes.

  • 0

I have an abstract base class that enforces certain operations on all derived classes. In addition to this, I would like to enforce certain other operations that are specific to subclasses declared in the derived classes.

The following is a minimal example:

class Base {
    public:
        virtual void init() = 0;
        virtual void reset() = 0;
};

class Derived1 : public Base {
    class Data {
        int *x1;
    public:
        Data() {
            x1 = NULL;
        }

        void alloc(int num) {
            x1 = new int[num];
        }

        ~Data() {
            delete[] x1;
            x1 = NULL;
        }
    } data;

public:
    void init() { ... }
    void reset() { ... }

    void resetData() { 
        data.~Data(); 
    }
};

class Derived2 : public Base {
    class Data {
        float *x2;
    public:
        Data() {
            x2 = NULL;
        }

        void alloc(int num) {
            x2 = new float[num];
        }

        ~Data() {
            delete[] x2;
            x2 = NULL;
        }
    } data;

public:
    void init() { ... }
    void reset() { ... }

    void resetData() { 
        data.~Data(); 
    }
};

In the example above Base enforces the init() and reset() methods on all derived classes.

In addition to this I would like to enforce that all derived classed have

  1. a member variable of named data and
  2. a method called resetData() that calls the destructor on this variable
  3. a method called Data &getData() that gets a reference to the variable

What would be the best way to achieve this?

  • 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-02T03:05:59+00:00Added an answer on June 2, 2026 at 3:05 am

    Thanks for your responses. This is what I have come up with. I know I am breaking my original requirements i.e. Data must be a subclass of the derived classes, but the following design pattern ensures that all derived classes have a data member variable, each of a different type, but again, each of these have some basic methods enforced on them. I think this is what @emsr suggested in one of his comments.

    Also now resetData() is done in a separate method. Thanks to @LuchianGrigore for pointing out a possible problem with explicitly calling the destructor. Now this method explicitly instead. The virtual destructor will also call the same function. I know that I shouldn’t call virtual functions from a destructor but by setting the scope of the function explicitly, I hope I am avoiding any ambiguity here. (Or is this also a problem?)

    struct Data {
        virtual void resetData() = 0;
        virtual ~Data() {}
    };
    
    template<typename _DT>
    class Base {
    protected:
        _DT data;
    
    public:
        _DT &getData() {
            return data;
        }
    
        void resetData() {
            data.resetData();
        }
    
        virtual void init() = 0;
        virtual void reset() = 0;
    };
    
    struct Data1 : public Data {
        int *x;
    
        Data1() {
            x = NULL;
        }
    
        void alloc(int num) {
            x = new int[num];
        }
    
        virtual void resetData() {
            delete[] x;
            x = NULL;
        }
    
        virtual ~Data1() {
            Data1::resetData();
        }
    
    };
    
    class Derived : public Base<Data1> {
    public:
        virtual void init() {
            // Carry out other init operations
            Data1 &x = getData();
            x.alloc(10);
        }
    
        virtual void reset() {
            // Carry out other reset operations
            data.resetData();
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract base class that many classes extend. I'd like all these
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have an abstract immutable base class that defines enforces child classes to be
Let's say I have an abstract base class that looks like this: class StellarObject(BaseModel):
I have a abstract base class that I have many inherited classes coming off
I have a abstract base class A and a set of 10 derived classes.
In my project, I have an abstract base class Base. I would like to
I have an abstract base class from which many classes are derived. I want
Imagine I have abstract base class Shape , with derived classes Circle and Rectangle
I have two case classes that inherit from an abstract base class. I want

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.