1

I am programming in c++11 and boost and i am trying to implement some kind of framework where someone just needs to inherit from class Base and implement method(), which may depend on other inheritances. These inheritances should then be automatically created in the main function, without any modification by the programmer.

class Base
{
public:
   virtual void method() = 0;
}

class A : public Base
{
public:
   static int state = 0;

   void method()  //
   {
      //do something with B
      A::state = B::state * 3;
   }
}

class B : public Base
{
public:
   static int state = 0;

   void method() //
   {
      //do something with A
      B::state = A::state * 2;
   }
}

int main()
{

//Access to an Array containing ONE pointer to each Inheritance

vector<Base*> inheritancesOfBase;
inheritancesOfBase.push_back(new A); // <- should be done automatically 
inheritancesOfBase.push_back(new B); // <- should be done automatically 

//PSEUDOCODE
for_each(base* pInh in inheritancesOfBase)
{

    pInh->method();
    clones.push_back(pInh->clone());

}


return 0;
}

I think this should be doable with some fancy metaprogramming, but how?

EDIT: clarification

Paul
  • 23
  • 4
  • It's not clear what you really want - to create some As and Bs depending on something at runtime, or to create new types of classes at runtime (using compile time metaprogramming...?) – doctorlove Jul 22 '13 at 09:45
  • In the end i would like to have an array containing one instance (or pointer) of each inheritance. – Paul Jul 22 '13 at 10:42
  • I think i have found a solution to my problem [here][1] [1]: http://stackoverflow.com/questions/17410942/c-automatic-instantiation-of-derived-classes – Paul Jul 23 '13 at 12:24
  • cool. Feel free to answer your own question – doctorlove Jul 23 '13 at 12:25

2 Answers2

0

First of all: Your code will not compile as you are missing the defintion of the statics. You need to add int A::state = 0 (or similar) to the code and the same for B::state.

Second, an automatic creation of A and B objects is not possible, as the compiler cannot know, what you intentions with the code are. Hence, you need at least to have some declaration of A and B objects in the main. E.g. how should the compiler know, that you need exactly one A and five B objects? So you must state your intention to the comiler:

int main(void)
{
    A myA;
    B myBArray[5];
    vector<shared_ptr<Base>> myABCollection;
}
ogni42
  • 1,232
  • 7
  • 11
0

So, we have something like

class Base
{
public:
   virtual void method() = 0;
};

(Don't forget the semicolons after the classes) and lots of subclasses, not just the A and B shown.

We want main to do something like

for(Base & item : instances)
{
    item.method();
}

You could make a factory, that keeps track of what it creates in a vector<shared_ptr<Base>> and return the instances when asked.
That solves half the problem. If you want to find all classes that derives from Base, given C++ doesn't have reflection I can't see how you'd do that. You could piece something together using clang and generate some code that way. The RecusiveASTVisitor might help.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • Thats exactly what i meant. – Paul Jul 22 '13 at 11:57
  • Basically what i wanted to avoid is that the programmer, who implements a new method, needs to propagate its existence somewhere else in the project... Maybe i am running in a basic design issue of my project? – Paul Jul 22 '13 at 12:05
  • I'm not clear what you mean - this is turning into chat. Perhaps we should take it to chat – doctorlove Jul 22 '13 at 12:06