数字*对象
对象*数字
可声明为友元函数
friend Vector operator*(const Vector& another, double num) {
Vector temp = another;
temp.end = temp.beg * num;
return temp;
}
friend Vector operator*(double num, const Vector& another) {
Vector temp = another;
temp.end = temp.beg * num;
return temp;
}
最后定义
friend ostream& operator<<(ostream& out, const Vector& another) {
out << "from " << another.beg << " to " << another.end << endl;
return out;
}
则可以实现:
Vector v1(p1, p2);
cout << 3*v1*4 << endl;