开发者

Passing class template as a function parameter

开发者 https://www.devze.com 2023-01-06 03:14 出处:网络
This question is a result of my lack of understanding of a situation, so please bear if it sounds overly stupid.

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:

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) {

    }
}
0

精彩评论

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