From looking at your comment code:
NumericSet<T> add(arma::Mat<T> mat, int option) {
if (option == 1) {
arma::Mat<T> sum = this->data + mat;
return new NumericSet<T>(sum);
} else {
this->data = this->data + mat;
return this;
}
}
it looks like your real goal is to allow mutation in place or producing a new value. This is why there is a distinction between operator+=
(for in-place operation) and operator+
(for not-in-place operation). Per the basic operator overloading idioms what you really want is to overload operator+=
(as a member), then overload operator+
in terms of it (as a non-member), something along these lines:
template<typename T>
class NumericSet {
...
NumericSet<T>& operator+=(const arma::Mat<T>& mat) {
// Changed from this->data = this->data + mat
// As a rule, += is always cheaper than +, so as long as data
// is solely owned by this, mutating in place is likely to be much more efficient
this->data += mat;
return *this;
}
}
template<typename T>
inline NumericSet<T> operator+(NumericSet<T> lhs, const arma::Mat<T>& rhs)
{
lhs += rhs;
return lhs;
}
Now you can just use +
when you would have used option=1
and +=
when you would have used some other option
value.