简体   繁体   中英

Passing a template class as an argument to a function

If I define a class using a template such as

template<class K, class V>
class myclass {
...
};

is there a way to pass objects defined by myclass to functions without using a template for the function? In order words, for every function that accepts myclass objects, does it also need to be defined using template< class K, class V> ?

The main reason for this is that I would like define a set of static functions that act on myclass objects so that I may limit the scope of these functions within their cpp files and not in header files.

Make your template class inherit from some base class:

template<class K, class V>
class myclass : mybaseclass { ... };

where the base class declares the public functionality of the template as pure virtual. This way you only have one function rather than one function for each K , V combination.

No, you cannot. A class template is not a class -- it is only a template. Only once you plug in the template parameters do you get a type, and for each different set of parameters you get a different , unrelated type.

Perhaps it's feasible for you to run some sort of type-erasing scheme, whereby you have a single container class which contains an abstract member pointer, and for each type you instantiate a concrete derived object. (Check out Boost.any.)

A bit like this:

class MyClassHolder { };

template <typename K, typename V>
class MyClassConcrete {  };

class MyClass
{
  MyClassHolder * p;

public:
  template <typename K, typename V> init(...)  // could be a constructor
  {
    p = new MyClassConcrete<K, V>();
  }
};

Now you can make your function accept a MyClass , but you have to add enough virtual functions to MyClassHolder , implement them in MyClassConcrete and expose them in MyClass that you can realise all your desired semantics.

不,您也必须使函数模板化(当然,除非将它们限制为仅使用myclass的特定专业化)。

You could do this:

void myfunc(const myclass<int, int>& mc) {}

Only if you wanted to be able to pass any type to your myclass argument in your function, would you need to make that myfunc a template too.

Yes, if you want your functions to be able to accept any instantiation of your template class they too must be template (typically with the same template parameters that are used for the class).

On the other hand, you can have your template class inherit from a non-template class that still allows you to operate the derived class via virtual functions. Also, to hide the type of your class and avoid riddling all your code of template s, there are the several techniques of type erasure .

由于C ++的静态性质,您必须为源中的每个(K,V)对实例化该函数的副本,因为编译器将不得不生成代码以不同地访问每个对的成员。

尽管我不清楚您的原因,是的,每个将myclass作为参数的函数也必须在K和V上进行模板化。如果设法抽象基础,则可以使每个myclass< K, V > (对于K和V的每种组合,这是不同的类型)(继承自实现该功能的单个基类,或通过虚函数转发)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM