Answer the question
In order to leave comments, you need to log in
How to provide an implicit call of the template class constructor in the overridden operator?
First, let's create a class that stores a numeric value:
class MyVal {
public:
int val_;
MyVal(int val = 0) : val_(val) {}
};
MyVal operator *(const MyVal &x, const MyVal &y)
{
return MyVal(x.val_ * y.val_);
}
MyVal a, b;
b = 1 * a;
template<class T>
class MyVal {
public:
T val_;
MyVal(T val = 0) : val_(val) {}
};
template<class T>
MyVal<T> operator *(const MyVal<T> &x, const MyVal<T> &y)
{
return MyVal<T>(x.val_ * y.val_);
}
void Test()
{
MyVal<int> a, b;
b = 1 * a;
}
Answer the question
In order to leave comments, you need to log in
It looks like the best option is a specialization:
MyVal<int> operator *(const MyVal<int> &x, const MyVal<int> &y)
{
return MyVal<int>(x.val_ * y.val_);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question