简体   繁体   English

将类模板作为函数参数传递

[英]Passing class template as a function parameter

This question is a result of my lack of understanding of a situation, so please bear if it sounds overly stupid. 这个问题是我对情况缺乏了解的结果,因此,如果听起来过分愚蠢,请耐心等待。

I have a function in a class, like: 我在一个类中有一个函数,例如:

Class A {

void foo(int a, int b, ?)
{
 ----
 }
}

The third parameter I want to pass, is a typed parameter like 我要传递的第三个参数是像

classA<classB<double >  > obj

Is this possible? 这可能吗? If not, can anybody please suggest a workaround? 如果没有,可以有人建议解决方法吗? I have just started reading about templates. 我刚刚开始阅读有关模板的信息。

Thanks, 谢谢,
Sayan 萨扬

Doesn't it work if you just put it there as a third parameter? 如果只是将它作为第三个参数放在那儿行不通吗?

void foo(int a, int b, classA< classB<double> > obj) { ... }

If it's a complex type it might also be preferable to make it a const reference, to avoid unnecessary copying: 如果是复杂类型,最好将其设为const引用,以避免不必要的复制:

void foo(int a, int b, const classA< classB<double> > &obj) { ... }

You can use a member template: 您可以使用成员模板:

Class A{

template <typename T>
void foo(int a, int b, T &c) {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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