4

I may be asking this in a odd way, but I'm not sure how else to ask.

I want to have a list of classes, not objects. This way I can call static functions without have to create the object.

2
  • Can you elaborate a bit more? What are you planning on doing with these classes? What static functions are you trying to call? Is this at compile-time or at runtime? Commented Jan 29, 2011 at 5:47
  • I want to register a list of classes to a list in a factory class. Iterate over those classes to look for the one I need (using static methods) then instantiate it and return it. Commented Jan 29, 2011 at 13:29

5 Answers 5

4

I would really prefer function pointers at this point:

struct A
{
  void SomeFunc(int);
};

struct B
{
  void AnotherFunc(int);
};


typedef void (*Function)(int);

std::vector<Function> vec;

vec.push_back(A::SomeFunc); vec.push_back(B::AnotherFunc);

for (Function f: vec)
{
  f(2);
}

Note that a static function is more or less equivalent to a traditional C-function (it just got to access some more scope).

Sign up to request clarification or add additional context in comments.

1 Comment

I chose this route so I didn't have to use boost and I could practice implementing function pointers. Thanks
2

What you are looking for are boost typelists. I would however not recommend diving into the Boost MPL if you are not already very experienced with templates, and know how many of their intricacies work.

Now for a simple home-made implementation:

struct Null {};

template <typename Type, typename Next>
struct List
{
  typedef Type Type;
  typedef Next Next;
};

//Now you can make lists like so:
typedef List<int, List<float List<short, Null> > > MyList;

From there use recursive Templated implementations to call the static methods you want.

If you want more information on these kinds of techniques, you should read Modern C++ Design

2 Comments

There really should be more elegant solutions to these problems.
@AlexanderRafferty: 0x has variadic templates.
1

As a solution you can create a list of method pointers

Comments

0

I don't think what you are asking is possible, at least not in the way you think. You cannot have a variable, array, container class or any other storage of type names. So you cannot do something like

ListOfClasses[n]::someStaticMember(...);

in C++. It is impossible.

Comments

0

http://www.boost.org/doc/libs/1_45_0/libs/mpl/doc/refmanual/refmanual_toc.html

eg:

typedef vector<C1,C2,C3> types;
at_c<types,0>::type::method();
...

2 Comments

Your link isn't very helpful. Atleast explain what the're supposed to do with it.
Also, I really wouldn't recommend using mpl::vector; or using namespace mpl;. The potential for confusion with std::vector is just too great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.