在 C++ 中是否可以从类成员函数返回类的元素?

jad*_*ade 0 c++ class function member

在 C++ 中是否可以从类成员函数返回类的元素?

namespace translation2d
{
    class translation2d
    {
        public:
           double x;
          double y;

        public:
            translation2d()
            {
                x=0;
                y=0;
            }

        public:
            translation2d translateBy(translation2d other); 
            //being interpreted as a constructor?
    }

    translation2d translation2d::translateBy(translation2d other)
    {//I need this member function to return an object of type translation2d
        x+=other.x;
        y+=other.y;
    }
};
Run Code Online (Sandbox Code Playgroud)

650*_*502 5

当然,有类似的东西

struct Translation2d {
    double x, y;
    Translation2d(double x, double y) : x(x), y(y) {}
    Translation2d translate_by(const Translation2d& other) {
        return Translation2d(x + other.x, y + other.y);
    }
};
Run Code Online (Sandbox Code Playgroud)