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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:49:28+00:00 2026-05-15T23:49:28+00:00

I have an abstract base class class IThingy { virtual void method1() = 0;

  • 0

I have an abstract base class

class IThingy
{
  virtual void method1() = 0;
  virtual void method2() = 0;
};

I want to say – “all classes providing a concrete instantiation must provide these static methods too”

I am tempted to do

class IThingy
{
  virtual void method1() = 0;
  virtual void method2() = 0;
  static virtual IThingy Factory() = 0;
};

I know that doesnt compile, and anyway its not clear how to use it even if it did compile. And anyway I can just do

Concrete::Factory(); // concrete is implementation of ITHingy

without mentioning Factory in the base class at all.

But I feel there should be some way of expressing the contract I want the implementations to sign up to.

Is there a well known idiom for this? Or do I just put it in comments? Maybe I should not be trying to force this anyway

Edit: I could feel myself being vague as I typed the question. I just felt there should be some way to express it. Igor gives an elegant answer but in fact it shows that really it doesn’t help. I still end up having to do

   IThingy *p;
    if(..)
       p = new Cl1();
    else if(..)
       p = new Cl2();
    else if(..)
       p = new Cl3();
    etc.

I guess reflective languages like c#, python or java could offer a better solution

  • 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-15T23:49:28+00:00Added an answer on May 15, 2026 at 11:49 pm

    The problem that you are having is partly to do with a slight violation a single responsibility principle. You were trying to enforce the object creation through the interface. The interface should instead be more pure and only contain methods that are integral to what the interface is supposed to do.

    Instead, you can take the creation out of the interface (the desired virtual static method) and put it into a factory class.

    Here is a simple factory implementation that forces a factory method on a derived class.

    template <class TClass, class TInterface>
    class Factory {
    public:
        static TInterface* Create(){return TClass::CreateInternal();}
    };
    
    struct IThingy {
        virtual void Method1() = 0;
    };
    
    class Thingy : 
        public Factory<Thingy, IThingy>,
        public IThingy {
            //Note the private constructor, forces creation through a factory method
            Thingy(){}
    public:
            virtual void Method1(){}
            //Actual factory method that performs work.
            static Thingy* CreateInternal() {return new Thingy();}
    };
    

    Usage:

    //Thingy thingy; //error C2248: 'Thingy::Thingy' : cannot access private member declared in class 'Thingy'
    
    IThingy* ithingy = Thingy::Create(); //OK
    

    By derinving from Factory<TClass, TInterface>, the derived class is forced to have a CreateInternal method by the compiler. Not deifining it will result in an error like this:

    error C2039: ‘CreateInternal’ : is not
    a member of ‘Thingy’

    • 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
Say we have an abstract base class IBase with pure virtual methods (an interface).
Say I have a abstract base class and I want to have a pure
I have an abstract base class from which many classes are derived. I want
Lets say I have an abstract base class with a pure virtual that returns
Let's say I have an abstract base class Base with a virtual function doSomething()
I have an abstract base class that enforces certain operations on all derived classes.
I have an abstract base class called Party. There are several concrete subclasses (Company,
I have an abstract base class and want to implement a function in the
I have an abstract base class that holds a Dictionary. I'd like inherited classes

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.