如何实现在实例之前可用的运算符(前置位置)

发布于 2024-11-19 03:02:40 字数 893 浏览 0 评论 0原文

我有一个带有 operator* 的类,它以标量作为参数,它允许我执行类实例与标量的乘法。我希望能够将标量乘以我的类的实例(具有相同结果的相反顺序)。我怎样才能做到这一点?

这是一个例子:

class Vector3d
{
public:
    Vector3d(double x, double y, double z) {
        v[0] = x; v[1] = y; v[2] = z;
    }

    template<typename T>
    Vector3d operator*(const T s) const {
        return( Vector3d( v[0] * s, v[1] * s, v[2] * s)); 
    }

//protected: example purpose
    double v[3];
};

main()
{
    double scalar = 2.0;
    Vector3d vector(1.0,2.0,3.0);
    Vector3d v2 = vector*scalar;
    //This is the operation I want to be able to perform !
    //Vector3d v3 = scalar*vector; 
    return 0;
}

我尝试像使用 ostream<< 运算符一样实现它,但没有成功......

template<typename T>
Vector3d operator*(T& s, const Vector3d &v)
{
    return( Vector3d( v[0] * s, v[1] * s, v[2] * s));
} 

I have a class with the operator* taking a scalar as argument, that allow me to perform the multiplication of an instance of my class with a scalar. I'd like to be able to multiply a scalar by an instance of my class (inverse order with the same result). How can I do that ?

Here an example :

class Vector3d
{
public:
    Vector3d(double x, double y, double z) {
        v[0] = x; v[1] = y; v[2] = z;
    }

    template<typename T>
    Vector3d operator*(const T s) const {
        return( Vector3d( v[0] * s, v[1] * s, v[2] * s)); 
    }

//protected: example purpose
    double v[3];
};

main()
{
    double scalar = 2.0;
    Vector3d vector(1.0,2.0,3.0);
    Vector3d v2 = vector*scalar;
    //This is the operation I want to be able to perform !
    //Vector3d v3 = scalar*vector; 
    return 0;
}

I tried to implement it like we do with ostream<< operator without success ...

template<typename T>
Vector3d operator*(T& s, const Vector3d &v)
{
    return( Vector3d( v[0] * s, v[1] * s, v[2] * s));
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-11-26 03:02:40

您必须将您的运算符 * 声明为具有相反参数顺序的非成员函数(外部类),并从中调用另一个函数

template<typename T>
Vector3d<T> operator*(T& s, const Vector3d<T> &v)
{
    return Vector3d(v.v[0] * s, v.v[1] * s, v.v[2] * s);
} 
template<typename T>
Vector3d<T> operator*(const Vector3d<T> &v, T& s)
{
    return s * v; //call the other overload
} 

并且不要忘记指定模板参数:

Vector3d<T>
        ^^^

还有一个问题...为什么要采用 T&T& code> 而不是 const T& 或者只是 T?在当前形式中,您正在阻止传递右值。例如,这不会编译:

Vector3d<int> v;
v*3; //3 isn't an lvalue, cannot bind to a nonconst reference

You must declare your operator* as a nonmember function (outside class) with inverse argument order and call the other one from it

template<typename T>
Vector3d<T> operator*(T& s, const Vector3d<T> &v)
{
    return Vector3d(v.v[0] * s, v.v[1] * s, v.v[2] * s);
} 
template<typename T>
Vector3d<T> operator*(const Vector3d<T> &v, T& s)
{
    return s * v; //call the other overload
} 

And don't forget specifying the template parameters:

Vector3d<T>
        ^^^

One more issue... Why take T& istead of const T& or just T? In current form you're preventing rvalues to be passed. For example, this wouldn't compile:

Vector3d<int> v;
v*3; //3 isn't an lvalue, cannot bind to a nonconst reference
堇年纸鸢 2024-11-26 03:02:40

最好在类之外进行运算符重载,这样可以提供最大的灵活性。

// 这编译得很好。

class Vector3d
{
public:
    Vector3d(double x, double y, double z) {
        v[0] = x; v[1] = y; v[2] = z;
    }

    double v[3];
};

template<typename T>
Vector3d operator*(const T& s, const Vector3d &v) 
{
    return( Vector3d( v.v[0] * s, v.v[1] * s, v.v[2] * s)); 
}

int main(int argc, char **argv)
{

    double scalar = 2.0;
    Vector3d vector(1.0,2.0,3.0);
    Vector3d v3 = scalar * vector; 
    return 0;
}

It is best to do operator overloading outside of the class, that gives you maximum flexibility.

// This compiles fine.

class Vector3d
{
public:
    Vector3d(double x, double y, double z) {
        v[0] = x; v[1] = y; v[2] = z;
    }

    double v[3];
};

template<typename T>
Vector3d operator*(const T& s, const Vector3d &v) 
{
    return( Vector3d( v.v[0] * s, v.v[1] * s, v.v[2] * s)); 
}

int main(int argc, char **argv)
{

    double scalar = 2.0;
    Vector3d vector(1.0,2.0,3.0);
    Vector3d v3 = scalar * vector; 
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文