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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:13:19+00:00 2026-06-12T06:13:19+00:00

I have a Class (MyFactory) which produces objects of type A. (myFactory will be

  • 0

I have a Class (MyFactory) which produces objects of type A. (myFactory will be an instance)
I want to allow users to extends the class A (to a Class B, for example), and override virtual methods of A.

For example, users who don’t want to extend A :

A* myObject = myFactoryInstance.createObject();

And my current trick for users which extends A with B :

B* myObjectPrepare = new B()
myFactoryInstance.setNextObject( myObjectPrepare );
A* myObject = myFactoryInstance.createObject(); // Will look for a "next_object"
     /* Here, myObject (which is myObjectPrepare modified by createObject) 
        methods will dynamically link to B class methods. */

My goal is to facilitate this operation.

.

I tried :

  template <class T>
  void MyFactory::set_requests_class() {
      new T(); 
      /* I don't want an instance, now. Just memorize class type
         at compilation time, and make instances of T along exec
      */
  }

(usage : set_requests_class(); Problem : I don’t want to produce instance)


That could be a solution :

myFactoryInstance.createObject<YourObjectTypeHere>();

But I don’t want to make the template parameter mandatory (default parameter for an attribute is only possible since c++11) : I want to keep it very simple for users who don’t want to extends A.
I CAN’T modify “createObject” signature, for the same reason.

(My future trick will be to instanciate the B object, keep it as attribute and clone it for new instances)

Thank you in advance (any suggestion will be appreciated)

  • 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-12T06:13:20+00:00Added an answer on June 12, 2026 at 6:13 am

    The easiest way is to just overload createObject. See this example:

    class A
    {
    public:
        A() { std::cout << "A"; }
    };
    
    class Subclass1 : public A
    {
    public:
        Subclass1() { std::cout << "Subclass1"; }
    };
    
    class AFactory
    {
    public:
        A* createInstance()
        {
            return new A();
        }
        template<typename Tsubclass>
        A* createInstance()
        {
            return new Tsubclass();
        }
    };
    
    int main()
    {
        AFactory fact;
        A* pa = fact.createInstance();// creates A
        A* psubclass1 = fact.createInstance<Subclass1>();// creates Subclass1
    
        delete pa;
        delete psubclass1;
        return 0;
    }
    

    And if you need to keep your design, where you set the type to create before you actually create the objects, then you can do it using a secondary factory like below. It’s not a very nice design though; basically the factory class now basically just acts as a wrapper that casts the result. Seems like there’s a better design from the outside.

    struct A
    {
        A() { std::cout << "A"; }
    };
    
    struct Subclass1 : A
    {
        Subclass1() { std::cout << "Subclass1"; }
    };
    
    struct GenericAFactoryBase
    {
        virtual A* createInstance() = 0;
    };
    
    template<typename Tsubclass>
    struct GenericAFactory : GenericAFactoryBase
    {
        A* createInstance() { return new Tsubclass(); }
    };
    
    struct AFactory
    {
        std::shared_ptr<GenericAFactoryBase> genericFactory;
    
        AFactory()
        {
            genericFactory.reset(new GenericAFactory<A>());
        }
    
        A* createInstance()
        {
            return genericFactory->createInstance();
        }
        template<typename Tsubclass>
        void SetType()
        {
            genericFactory.reset(new GenericAFactory<Tsubclass>());
        }
    };
    
    int main()
    {
        AFactory fact;
        A* pa = fact.createInstance();// creates A
    
        fact.SetType<Subclass1>();
        A* psubclass1 = fact.createInstance();// creates Subclass1
    
        delete pa;
        delete psubclass1;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Class and Student objects. Both have collection of another as property. Which
I have a factory class that populates objects with data. I want to implementing
I want to create a Factory which returns dao-instance, depending on the Class clazz
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have class Meat extends Food { $var = Food::foodFunction… } I need to
We have class lua. In lua class there is a method registerFunc() which is
I have class like this below shown. which contains the shopping items where the
I have a class EnumConverter<T extends Enum<?>> that converts strings to the correct enum
I have an Assembly(A) which defines a Managed class which has a public constructor
I have a factory that is supposed to create objects that inherit from class

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.