模板类包含对其自身实例的引用的stdvector [英] template class containing a stdvector of references to its own instances

查看:224
本文介绍了模板类包含对其自身实例的引用的stdvector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我第一次尝试构建一个复杂的模板结构。

I'm relatively new to C++, and am trying for the first time to build a complex template structure.

我如何声明,作为模板的成员Foo类,Foo *元素的std :: vector,但是可以是各种类型?

How can I declare, as member of a template class Foo, a std::vector of Foo* elements, but that could be of various types?

#include <vector>

template <typename T>
class Foo {
    T mValue;
    std::vector< Foo<T>* > mFooParameters;  // <---- I would like this vector to contain
                                            //       any sort of Foo<T>* elements,
                                            //       Foo<int>*, Foo<double>*, etc.
};

简单,可能但复杂或不可能?

Is it straightforward, possible but complicated, or impossible?

感谢您的回答!

推荐答案

如果您的向量类型都是相关的,请使用@jogojapan提供的链接中解释的多态性相关的,使用 void * 的向量来保存指向您的数据的指针(虽然有点杂乱)

If your vector types are all related, use polymorphism as explained in the link provided by @jogojapan, if the types are not related at all, use a vector of void* to hold pointers to your data (kind of messy though)

这篇关于模板类包含对其自身实例的引用的stdvector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆