A
aaragon
Hi,
I'm designing a Matrix class so I read what Bjarne Stroustrup has on
section 22.4.6 about it. It shows that it is possible to eliminate the
temporaries by delaying the construction of the object. In his example
(page 675):
struct MVmul {
const Matrix& m;
const Vector& m;
MVmul(const Matrix& mm, const Vector& vv) : (mm), v(vv) { }
operator Vector(); // evaluate and return result
};
inline MVmul operator*(const Matrix& mm, const Vector& vv)
{
return MVmul(mm,vv);
}
Now, I think that's cool, however, I have a question about the
converting function operator Vector(). The implementation could be
done there OR it can be done as a constructor of the Vector class,
right? Something like this:
class Vector {
// member variables
...
public:
// constructors
...
Vector(const MVmul& mvmul) {
// evaluate
}
...
// rest of the class
};
Now the question is, is an approach better than the other? I think
that both are equivalent, but I wanted to make sure asking to the
experts...
Thank you,
a²
I'm designing a Matrix class so I read what Bjarne Stroustrup has on
section 22.4.6 about it. It shows that it is possible to eliminate the
temporaries by delaying the construction of the object. In his example
(page 675):
struct MVmul {
const Matrix& m;
const Vector& m;
MVmul(const Matrix& mm, const Vector& vv) : (mm), v(vv) { }
operator Vector(); // evaluate and return result
};
inline MVmul operator*(const Matrix& mm, const Vector& vv)
{
return MVmul(mm,vv);
}
Now, I think that's cool, however, I have a question about the
converting function operator Vector(). The implementation could be
done there OR it can be done as a constructor of the Vector class,
right? Something like this:
class Vector {
// member variables
...
public:
// constructors
...
Vector(const MVmul& mvmul) {
// evaluate
}
...
// rest of the class
};
Now the question is, is an approach better than the other? I think
that both are equivalent, but I wanted to make sure asking to the
experts...
Thank you,
a²