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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:06:48+00:00 2026-05-26T03:06:48+00:00

I have 6 classes which all perform the same actions. I would like to

  • 0

I have 6 classes which all perform the same actions. I would like to move common behavior to a common [base] class.

There are actions to be performed on 6 separate objects. The six objects are located in derived classes. Intuitively, the private member objects would be accessed through the child (derived class) in the base class.

What is the C++ pattern I am looking for?

class Base
{
  // Common behavior, operate on m_object
  ...
  void Foo()
  {
    m_object.Bar();
  }
};

class Derived1 : public Base
{
  // No methods, use Base methods

  private:
  MyObject1 m_object;
}

class Derived2 : public Base
{
  // No methods, use Base methods

  private:
  MyObject2 m_object;
}

The thing that is boxing me into this situation is MyObject1, MyObject2, etc offer Bar(), but don’t share a common base class. I really can’t fix the derivation because the objects come from an external library.

  • 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-05-26T03:06:48+00:00Added an answer on May 26, 2026 at 3:06 am

    If they are introduced in the derived classes, then the base class cannot directly access them. How would the base class know that all derived classes have a specific member?

    You could use virtual protected methods like so:

    class my_base
    {
    protected:
        virtual int get_whatever();
        virtual double get_whatever2();
    private:
        void process()
        {
           int y = get_whatever();
           double x = get_whatever2();
           //yay, profit?
        }
    }
    
    class my_derived_1 : my_base
    {
    protected:
        virtual int get_whatever()
        {
            return _my_integer;
        }
    
        virtual double get_whatever2()
        {
           return _my_double;
        }
    }
    

    Another possibility (if you want to call the base methods from the derived classes) is to simply supply the arguments to the base methods.

    class my_base
    {
    protected:
        void handle_whatever(int & arg);
    };
    
    class my_derived : my_base
    {
        void do()
        {
          my_base::handle_whatever(member);
        }
    
        int member;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which depends on 3 classes, all 3 of which have
I have a superclass which instantiates my Sql class so all derived classes will
I have various classes which all contain address details, i.e. AddressLine1, AddressLine2, AddressLine3, Suburb,
I have a set of some classes which are all capable of being constructored
I have 6 classes, which are all document types. All the document types have
I have some functionality that I need in all my classes which derive from
I have the following 3 rails classes, which are all stored in one table,
I have an interface called ICommand from which various classes inherit. These classes all
I have an interface for a variety of classes, all of which should implement
I have classes which have automatic properties only like public customerName {get; set;}. They

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.