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 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

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't call a servlet directly from a JSP. However,… May 16, 2026 at 5:21 pm
  • Editorial Team
    Editorial Team added an answer I don't think you can do that with SQL Server's… May 16, 2026 at 5:21 pm
  • Editorial Team
    Editorial Team added an answer Subscribe to the feeds in Google Reader, give them a… May 16, 2026 at 5:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have an abstract base class and I want to declare a field or
I have an abstract base class which acts as an interface. I have two
I have an abstract base class with a TcpClient field: public abstract class ControllerBase
I have an abstract base class and derived class: type TInterfaceMethod = class public
I have an abstract base class called Shape from which both Circle and Rectangle
I have an abstract base class class AbstractClass { Col<AbstractClass> parent public AbstractClass() {
I have an abstract base class which contains a private nested implementation. visual c++
I have an abstract base class (Comparable) with Date and Time virtually inheriting from
I have an abstract class with a single concrete method. In this method I
I have a generic abstract base class from which I would like to derive

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.