开发者

How to store TypeInfo

  class A {}
  A a;
  type_info info = typeid (a); // error type_inf开发者_StackOverflow社区o is private

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


You can't create copies of 'type_info' objects. However, the result if 'typeid' is an Lvalue and the corresponding 'type_info' objects, once obtained, continue to live till the end of the program. For these reasons, you can safely store pointers to 'type_info' objects in your list.


You cannot instantiate objects of the type_info class directly, because the class has only a private copy constructor. Since the list needs 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 to an alternative design, not involving RTTI, if possible.


From your comment to Cătălin Pitiș' answer, I understand that your goal is to write a function that returns a different "Style" type for different "Page" types. Does this have to be dynamic? If not, would something like this do what you want?

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();
}

Or, with Boost.MPL:

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

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

Getting the Style type from the Page type is:

boost::mpl::at<PageToStyle, Page>::type;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜