How to store TypeInfo

  class A {}
  A a;
  type_info info = typeid (a); // error type_info is private

I need a list list<type_info>to store the type of classes. Is there a solution?

+5
source share
3 answers

You cannot create type_info class objects directly, because the class has only a private copy constructor. Since the list needs a copy constructor ...

If you really need it, use std :: list <type_info *>.

I don’t know why you need this list, but I would think of an alternative design without using RTTI, if possible.

+4
source

'type_info'. , "typeid" - Lvalue "type_info", . 'type_info' .

+8

Cătălin Pitiş , - , "" "". ? , - , ?

template<class PageT>
struct StyleOf;

template<>
struct StyleOf<PageA>{
    typedef StyleA type;
};

template<>
struct StyleOf<PageB>{
    typedef StyleB type;
};

// etc...

template<class PageT>
typename StyleOf<PageT>::type
GetStyle(const PageT&){
    return StyleOf<PageT>::type();
}

, Boost.MPL:

using boost::mpl::map;
using boost::mpl::pair;

typedef map<
    pair<PageA, StyleA>,
    pair<PageB, StyleB>,
    //etc.
>
PageToStyle;

:

boost::mpl::at<PageToStyle, Page>::type;
+1

All Articles