Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static abstract methods in c++

Tags:

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

like image 888
pm100 Avatar asked Jul 22 '10 21:07

pm100


People also ask

What are static and abstract methods?

Declaring abstract method static If you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.

Can abstract methods have static methods?

Yes, abstract class can have Static Methods. The reason for this is Static methods do not work on the instance of the class, they are directly associated with the class itself.

What is static abstract in C#?

Static abstract members allow each implementing member of an interface to implement their version of a static accessor that you can access via the Type handle. You can implement these members implicitly or explicitly, like any other interface definition.

Why can't I have abstract static methods in C #?

With static methods you need to go through a class name anyway, so the exact method to call is known at compile time because it can't and won't change. Thus, virtual/abstract static methods are not available in .


1 Answers

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'

like image 179
Igor Zevaka Avatar answered Oct 14 '22 07:10

Igor Zevaka