3

I have a base class and a few derivative. I have to 'register' some static function from each of them. Here is the example:

class Base
{
   // Some interface...
};

class Der1 : Base
{
   static void Do();
};
class Der2 : Base
{
   static void Do();
};

void processStatic()
{
   SomeFunc(Der1::Do);
   SomeFunc(Der2::Do);
}

As you see, SomeFunc receives function pointer. I want to do that automatically with each new derivative class, is it possible? Maybe, predefine static function in Base class and register it there. But I think it's impossible, yes?


Maybe, this will be more easier to understand what do I want:

class Der1 : Base
{
   Der1() { SomeFunc(Der1::Do); }
   static void Do();
};
class Der2 : Base
{
   Der2() { SomeFunc(Der2::Do); }
   static void Do();
};
Max Frai
  • 61,946
  • 78
  • 197
  • 306
  • What does `SomeFunc` do? Do you want it to be called every construction, like your second example, or only once, like presumed in your first example? Does it matter when? Does order matter? – GManNickG Apr 06 '11 at 20:05
  • Do you want to call the registration function once per class, or once per object? The first example implies the former; the second example implies the latter. – Robᵩ Apr 06 '11 at 20:06
  • @rob-adams once per class. @gman that function put function pointer into some list which I use later. – Max Frai Apr 06 '11 at 20:07
  • So what does the base class have to do with it? – GManNickG Apr 06 '11 at 20:19
  • @gman The main idea is that each Derivative class will add it's static method into some map of function pointers. – Max Frai Apr 06 '11 at 20:45
  • @Ockonal: I understand, but where is `Base` necessary? Or is that just noise with respect to the question? – GManNickG Apr 06 '11 at 21:03
  • @gman I just thought how to do that with base class. But seems it's possible without it. – Max Frai Apr 06 '11 at 21:04
  • @Ockonal: Okay, so you don't need `Base`? That was just an attempt? – GManNickG Apr 06 '11 at 21:05
  • @gman yes :) Any way to automatically call SomeFunc with each new derived class. – Max Frai Apr 07 '11 at 04:32

3 Answers3

2

EDIT: Completely replacing previous answer due to clarified requirements.

You could use the CRTP to declare a specialized base class that does nothing more than call your registration function:

#include <iostream>
void SomeFunc(void(*fp)()) {
  (*fp)();
};

template <class D>
struct ExtraBass {
  ExtraBass() {
    static bool once;
    if(!once)
      SomeFunc(D::Do);
    once = true;
  }
};

struct Bass {
};

struct Drive : Bass, ExtraBass<Drive>  {
  static  void Do() { std::cout << "Drive::Do\n"; }
};

struct Deride : Bass , ExtraBass<Deride> {
  static  void Do() { std::cout << "Deride::Do\n"; }
};

int main() {
  Drive d1;
  Deride d2;
  Deride d3;
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

This is not an easy thing to do in C++, but I'm not saying it's impossible. If all you need is a list of subclass names, these answers might help:

Somehow register my classes in a list

c++ List of classes without initializing them for use of static functions

Seems either macro magic or boost mpl is your tool of choice.

Community
  • 1
  • 1
rubenvb
  • 74,642
  • 33
  • 187
  • 332
0

I just wondering, if you did something like

void SomeFunc(void (*doFunc)())
{ 
   doFunc();
}

template <class T> int Register()
{
    SomeFunc(T::Do);
    return 0;
}

template <class T> class Base
{
   static int _i;
};

template <class T> int Base<T>::_i =  Register<T>();

class Derived : Base<Derived>
{
   public:
   static void Do() {  }
};
satnhak
  • 9,407
  • 5
  • 63
  • 81