C ++ List of classes without initializing them to use static functions

I can ask this in a strange way, but I'm not sure how to ask more.

I want to have a list of classes, not objects. That way I can call static functions without creating an object.

+4
source share
5 answers

I would prefer pointer functions at the moment:

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 needs to access another area).

+4
source

What you are looking for formats a list of types . However, I would not recommend diving into the Boost MPL if you are not already very experienced with templates and know how many of their subtleties 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, recursive template implementations are used to call the static methods you want.

If you need more information about these methods, you should read Modern C ++ Design.

+2
source

As a solution, you can create a list of method pointers

+1
source

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(); ... 
0
source

I do not think that you are asking, at least not in the way you think. You cannot have a variable, array, container class, or any other type name storage. Therefore, you cannot do something like

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

in C ++. It's impossible.

0
source

Source: https://habr.com/ru/post/1337569/


All Articles