C++: requiere función estática en clase abstracta

I am trying to write a c++ abstract class and I can't figure out how to require implementers of this class to contain a static function.

Por ejemplo:

class AbstractCoolThingDoer
{
    void dosomethingcool() = 0; // now if you implement this class 
                                        // you better do this
}

class CoolThingDoerUsingAlgorithmA: public AbstractCoolthingDoer
{
    void dosomethingcool()
    {
        //do something cool using Algorithm A
    }
}

class CoolThingDoerUsingAlgorithmB: public AbstractCoolthingDoer
{
    void dosomethingcool()
    {
        //do the same thing using Algorithm B
    }
}

Now I'd like to do the coolthing without the details of how coolthing gets done. So I'd like to do something like

AbstractCoolThingDoer:dosomethingcool();

without needing to know how the coolthing gets done, but this seems to require a function that is both virtual and static which is of course a contradiction.

The rationale is that CoolThingDoerUsingAlgorithmB may be written later and hopefully the softare that needs cool things done won't have to be rewritten.

EDIT:Not sure I was clear on what I'm trying to accomplish. I have 3 criteria that I'm looking to satisfy

  1. A library that uses abstractcoolthingdoer and does not need to be rewritten ever, even when another coolthingdoer is written that the library has never heard of.

  2. If you try to write a coolthingdoer that doesn't conform to the required structure, then the executable that uses the library won't compile.

  3. coolthingdoer has some static functions that are required.

I'm probably chasing down a poor design, so please point me to a better one. Am I needing a factory?

preguntado el 09 de septiembre de 13 a las 23:09

And how exactly would CoolThingDoerUsingAlgorithmB's implementation be picked up? -

You're allowed to provide implementations for pure virtual functions. So provide an (out-of-line) implementation of AbstractCoolThingDoer::dosomethingcool and derived classes will still be forced to override the virtual function, but the ones that don't care to do the cosa genial differently can still call the default base class implementation explicitly within their body. -

probably in this case you may just define pointer to 'void dosomethingcool()' and declare it as member in AbstractCoolThingDoer? -

How do you plan on distinguishing which implementation of dosomethingcool should be called by AbstractCoolThingDoer? -

@agbinfo I was thinking of having a template<class coolthingdoer> in there, with a dynamic_cast<A*>((B*)0) to enforce inheritance (will that work?) -

2 Respuestas

Maybe, something like this will help (see ideone.com example):

#include <iostream>

class A
{
 protected:
  virtual void do_thing_impl() = 0;
 public:
  virtual ~A(){}
  static void do_thing(A * _ptr){ _ptr->do_thing_impl(); }
};

class B : public A
{
 protected:
  void do_thing_impl(){ std::cout << "B impl" << std::endl; }
};

class C : public A
{
 protected:
  void do_thing_impl(){ std::cout << "C impl" << std::endl; }
};

int main() 
{
 B b_;
 C c_;

 A::do_thing(&b_);
 A::do_thing(&c_);  

 return (0);
}

EDIT: It seems to me the OP does not need run-time polymorphism, but rather compile-time polymorphism without need of class instance (use of static functions when the implementation is hidden in the derived classes, no instance required). Hope the code below helps to solve it (example on ideone.com):

#include <iostream>

template <typename Derived>
struct A
{
  static void do_thing() { Derived::do_thing(); }
};

struct B : public A<B>
{
  friend A<B>;
 protected:
  static void do_thing() { std::cout << "B impl" << std::endl; }
};

struct C : public A<C>
{
  friend A<C>;
 protected:
  static void do_thing() { std::cout << "C impl" << std::endl; }
};

int main() 
{
 A<B>::do_thing();
 A<C>::do_thing();

 return (0);
}

EDIT #2: To force fail at compile-time in case user does not adhere to desired pattern, here is the slight modification at ideone.com:

#include <iostream>

template <typename Derived>
struct A
{
  static void do_thing() { Derived::do_thing_impl(); }
};

struct B : public A<B>
{
  friend A<B>;
 protected:
  static void do_thing_impl() { std::cout << "B impl" << std::endl; }
};

struct C : public A<C>
{
  friend A<C>;
 protected:
  static void do_thing_impl() { std::cout << "C impl" << std::endl; }
};

struct D : public A<D>
{
 friend A<D>;
};

int main() 
{
 A<B>::do_thing();
 A<C>::do_thing();
 A<D>::do_thing(); // This will not compile.

 return (0);
}

Respondido el 10 de Septiembre de 13 a las 21:09

And the obvious follow-up question to OP is, why make it a static in the first place? You need to act on an instance, so why not just have a plain old non-static member function? - juanchopanza

@juanchopanza I don't know exactly. Maybe the author of class A wants to do some common pre/past processing in static ¿función? - vuelta

I guess what you both are saying is that in general one needs to choose either 1) instantiate an object to call it's functions even when that isn't intuitive or 2) use something besides inheritance to ensure that addon classes have the required structure? - OldMcFartigan

@user2762953 It depends of what are you trying to achieve. It seems to me from your follow-up question that you don't need run-time polymorphism, but rather compile-time polymorphism. - vuelta

@Petr Budnik Right. I need extensibility and in my (new to c++) mind I figure inheritance is the way to provide that while enforcing certain ground rules. But when i started putting pen to paper I started realizing that there are certain functions that "feel" static. That is didn't seem like they should require an instantiation of an object. So I don't know wether to instantiate dummy objects just to access their functions or take those functions outside objects. But then I don't know how to keep everything tidy. - OldMcFartigan

This looks to me like right place to implement patrón de puente. Maybe this is what you are (unconsciously) willing to achieve. In short you specify an interface and its implementations, then call to your do_thing method in turn calls an implementation on a pointer to implementer class.

Ejemplo de C ++

Respondido el 10 de Septiembre de 13 a las 00:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.