指针的部分专业化,C ++ [英] Partial specialization for pointers, c++

查看:123
本文介绍了指针的部分专业化,C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对GList类进行部分专业化,以便可以存储I(即I *)的指针?

How to make partial specialization of the class GList so that it is possible to store pointers of I (i.e I*) ?

template <class I>
struct TIList
{
    typedef std::vector <I> Type;
};


template <class I>
class GList
{
      private:
            typename TIList <I>::Type objects;
};

推荐答案

您无需专门为此允许.它已经可以存储指针了.

You don't need to specialise to allow that. It can store pointers already.

GList<int*> ints;

无论如何,如果要专门针对指针使用GList,请使用以下语法.

Anyway, if you wanted to specialise GList for pointers, use the following syntax.

template <class I>
class GList<I*>
{
    ...
};

然后像在任何普通模板中一样使用I.在上面使用GList<int*>的示例中,将使用指针专门化,而I将是int.

Then just use I as you would in any normal template. In the example above with GList<int*>, the pointer specialisation would be used, and I would be int.

这篇关于指针的部分专业化,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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