操作符和操作符重载

我书写了一个结构体,用它来表示向量,如果我想要实现向量的加法或者乘法,如果不使用操作符重载,那么在结构体中定义方法(函数)将是唯一的办法,并且这会有一点难以阅读

#include<iostream>
#include<string>

struct vector2 
{
    float x, y;
    vector2(float x,float y)
        :x(x), y(y) {}
    vector2 Add(const vector2& other)const
    {
        return vector2(x + other.x, y + other.y);
    }

    vector2 Multiplt(const vector2& other)const
    {
        return vector2(x * other.x, y * other.y);
    }
};
int main()
{
    vector2 position(4.0f, 4.0f);
    vector2 speed(0.5f, 1.5f);
    vector2 powerup(1.1f, 1.1f);

    vector2 result = position.Add(speed.Multiplt(powerup));
    std::cin.get();
}

操作符重载的写法类似于函数

struct vector2 
{
    float x, y;
    vector2(float x,float y)
        :x(x), y(y) {}
    vector2 Add(const vector2& other)const
    {
        return vector2(x + other.x, y + other.y);
    }
    vector2 operator+(const vector2& other)const
    {
        return Add(other);
    }
};

也可以这样写,让Add调用重载的操作符“+”

struct vector2 
{
    float x, y;
    vector2(float x,float y)
        :x(x), y(y) {}
    vector2 Add(const vector2& other)const
    {
        return *this+other;
    }
    vector2 operator+(const vector2& other)const
    {
        return vector2(x + other.x, y + other.y);
    }

};

也可以这么写

struct vector2 
{
    float x, y;
    vector2(float x,float y)
        :x(x), y(y) {}
    vector2 Add(const vector2& other)const
    {
        return operator+(other);
    }
    vector2 operator+(const vector2& other)const
    {
        return vector2(x + other.x, y + other.y);
    }

};

这样直接调用重载操作符就不会报错了

#include<iostream>
#include<string>

struct vector2 
{
    float x, y;
    vector2(float x,float y)
        :x(x), y(y) {}
    vector2 Add(const vector2& other)const
    {
        return operator+(other);
    }
    vector2 operator+(const vector2& other)const
    {
        return vector2(x + other.x, y + other.y);
    }

    vector2 Multiply(const vector2& other)const
    {
        return vector2(x * other.x, y * other.y);
    }
    vector2 operator*(const vector2& other)const
    {
        return Multiply(other);
    }

};

int main()
{
    vector2 position(4.0f, 4.0f);
    vector2 speed(0.5f, 1.5f);
    vector2 powerup(1.1f, 1.1f);

    vector2 result = position+speed*powerup;
    std::cin.get();
}

如果想要将结果打印在控制台上,直接调用打印符号会报错,需要对其进行重载

#include<iostream>
#include<string>

struct vector2 
{
    float x, y;
    vector2(float x,float y)
        :x(x), y(y) {}
    vector2 Add(const vector2& other)const
    {
        return operator+(other);
    }
    vector2 operator+(const vector2& other)const
    {
        return vector2(x + other.x, y + other.y);
    }

    vector2 Multiply(const vector2& other)const
    {
        return vector2(x * other.x, y * other.y);
    }
    vector2 operator*(const vector2& other)const
    {
        return Multiply(other);
    }

};

std::ostream& operator<<(std::ostream& stream, const vector2& other)
{
    stream << other.x << "," << other.y;
    return stream;
}
int main()
{
    vector2 position(4.0f, 4.0f);
    vector2 speed(0.5f, 1.5f);
    vector2 powerup(1.1f, 1.1f);

    vector2 result = position+speed*powerup;
    std::cout << result << std::endl;
    std::cin.get();
}

对等号进行重载,来判断两个向量是否相等

#include<iostream>
#include<string>

struct vector2 
{
    float x, y;
    vector2(float x,float y)
        :x(x), y(y) {}
    vector2 Add(const vector2& other)const
    {
        return operator+(other);
    }
    vector2 operator+(const vector2& other)const
    {
        return vector2(x + other.x, y + other.y);
    }

    vector2 Multiply(const vector2& other)const
    {
        return vector2(x * other.x, y * other.y);
    }
    vector2 operator*(const vector2& other)const
    {
        return Multiply(other);
    }

    bool operator==(const vector2& other)const
    {
        return x == other.x&&y == other.y;
    }

    bool operator!=(const vector2& other)const
    {
        return !(*this == other);
        //return !operator==(other);
    }

};

std::ostream& operator<<(std::ostream& stream, const vector2& other)
{
    stream << other.x << "," << other.y;
    return stream;
}
int main()
{
    vector2 position(4.0f, 4.0f);
    vector2 speed(0.5f, 1.5f);
    vector2 powerup(1.1f, 1.1f);

    vector2 result1 = position.Add(speed.Add(powerup));
    vector2 result2 = position+speed*powerup;
    std::cout << result2 << std::endl;

    if (result1 == result2)
    {

    }
    std::cin.get();
}

 

posted @ 2020-05-08 23:18  Wangtn  阅读(167)  评论(0编辑  收藏  举报