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

  • Home
  • SEARCH
  • 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 933885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:54:18+00:00 2026-05-15T20:54:18+00:00

I have two classes, one of which is a subclass of another, and differs

  • 0

I have two classes, one of which is a subclass of another, and differs only by the fact that it contains an additional member variable to its parent. I am not using the default constructor, passing in a reference to a single object as the constructors parameter. What I would like is for the constructor of the parent to examine this object, and then determine whether to construct an instance of the parent class (in most cases) or the subclass (in a few specialised cases).

class Superclass 
{ 
    public: 
        Foo foo;

        Superclass(MyObject* object) 
        { 
            foo = object->GetFoo();

            if(object->CreateSubclass())
            {
                //Create Subclass
            }
            else
            {
                //Create Superclass
            }
        } 
}; 

class Subclass : public Superclass 
{ 
    public: 
        Barr barr;

        Subclass(MyObject* object)
        { 
            barr = object->GetBarr();
        } 
};

I’m aware of the factory design pattern, but don’t want to have to have a factory object just for this. I’d rather duplicate the Superclass initialisation stuff into the Subclass (which seems bad) and then examine the object at each of the points where a Superclass is created and then call the appropriate constructor:

Superclass* class;
if(object->CreateSubclass())
{
    class = new Subclass(obj);
}
else
{
    class = new Superclass(obj);
}

Is this sort of thing possible, and if so how would I go about calling the subclasses constructor from Superclass constructor? I’ve tried making a call to Subclass(object), but I run into issues with both Superclass and Subclass needing to be defined before the other.

Thanks for any advice you can provide.

  • 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-15T20:54:19+00:00Added an answer on May 15, 2026 at 8:54 pm

    If you’re set against a factory class, why not just a static function?

    class Subclass
    {
    public:
        static Superclass* create(const MyObject* const object)
        {
            if (object->createSubclass())
            {
                return new Subclass(object);
            }
            else
            {
                return new Baseclass(object);
            }
        }
    
        // ...
    }
    
    // ...
    
    Superclass* const myInstance = Subclass::create(myObject);
    

    (I’ve put the static class in Subclass because it needs both the super and subclass implementations, but you could easily put it in a shared namespace or something, or even put the declaration in the base class but put the implementation in a cpp)

    You’ve now got most of the benefits of a factory class, with no more code than you had before. You’re just missing the ability to pass it around or easily refactor it into multiple implementations.

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

Sidebar

Related Questions

i have two data classes which hold only data members(no functions). One is CallTask
I have a class that instantiates two classes which implement interfaces. I want one
I have created two viewController classes such that one is superclass of another.i have
I have a class that contains objects of two other classes. I need one
I have two classes one of which inherits from the other. Im trying to
I have two projects in my solution; one which builds a static lib, another
Suppose I have two classes (one a parent and one a subclass). How do
Hi I have two classes, one called Instruction, one called LDI which inherits from
I have two classes, one which is hardware-dependent and one which is not (let's
I need to have two classes, one class has two methods each of which

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.