ostream和运算符重载

时间:2019-07-15 16:19:33

标签: c++

“我是c ++的新手,正在构建pong克隆,想知道什么是,如何使用重载运算符以及为什么使用它?”

class Ball
{
private:
    int x, y;
    int originalX, originalY;
    Dir direction;
public:
    Ball(int posX, int posY) 
    {
        originalX = posX;
        originalY= posY;
        x = posX; y = posY;
        direction = STOP;
    }
friend ostream& operator<<(ostream& o, Ball c)
    {
        o << "Ball [" << c.x << "," << c.y << "][" << c.direction << "]" << endl;
        return o;
    }
};

0 个答案:

没有答案