Sayan
Sayan

Reputation: 2724

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

Upvotes: 2

Views: 2019

Answers (2)

MarkD
MarkD

Reputation: 4954

You can use a member template:

Class A{

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

    }
}

Upvotes: 1

sth
sth

Reputation: 229593

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

Upvotes: 4

Related Questions