I want to use copy constructor with the result of my overloaded operator*
. I found the way to do this: by returning reference from the operator. What I would like to know:
- Is this okay?
Here is the example:
class Foo{
public:
double x,y,z;
Foo() : x(0), y(0), z(0) {};
Foo(double xi, double yi, double zi) : x(xi), y(yi), z(zi) {};
Foo(const Foo &bar) : x(bar.x), y(bar.y), z(bar.z) {};
};
Foo& operator * (const double &b, Foo &a){ // Yep, I want to modify a
a.x *= b;
a.y *= b;
a.z *= b;
return a;
}
int main(){
Foo temp;
Foo bar = 4*temp; // Here is what I want to do!
return 0;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…