Hello,
Can i make only one member function of a class generic using templates.
Code:class A
{
int member;
void func1()
{
}
template<class T>
void func2(T& obj);
};
template<class T>
void A:: func2(T& obj)
{
obj.val=0;
}
Printable View
Hello,
Can i make only one member function of a class generic using templates.
Code:class A
{
int member;
void func1()
{
}
template<class T>
void func2(T& obj);
};
template<class T>
void A:: func2(T& obj)
{
obj.val=0;
}
Yes. There are some conditions on being allowed to do that (eg such a function cannot be virtual, IIRC).
In your example, you might also want to use a predicate to ensure that type T has a member named val that can be set to zero, rather than just waiting until you use A::func2() on an object of arbitrary type. I'll leave researching that suggestion as a learning exercise.....