简体   繁体   English

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

[英]In C++ is it possible to return an element of the class from a class member function?

In C++ is it possible to return an element of the class from a class member function?在 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;
    }
};

Sure, with something like当然,有类似的东西

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);
    }
};

6502's answer is correct, and the code is neat. 6502的回答正确,代码整洁。

I just wanted to give a fix that minimises changes to your original code, for clarity:为了清楚起见,我只是想提供一个修复程序,以最大限度地减少对原始代码的更改:

translation2d translation2d::translateBy(translation2d other)
{
    translation2d copy = *this;

    copy.x += other.x;
    copy.y += other.y;

    return copy;
}

The idea is to:这个想法是:

  1. Create a copy of the "current" object so you don't impact the original (you don't want that)创建“当前”对象的副本,这样您就不会影响原始对象(您不希望那样)
  2. Return that copy once you've adjusted it with data from other使用other数据进行调整后返回该副本

Written another way, we can avoid copying the argument again since you already passed it by value:换句话说,我们可以避免再次复制参数因为您已经按值传递了它:

translation2d translation2d::translateBy(translation2d copy)
{
    copy.x += this->x;
    copy.y += this->y;

    return copy;
}

… though I'd expect the compiler to do this for you in reality, and I find this version of the code harder to read. ...虽然我希望编译器在现实中为你做这件事,但我发现这个版本的代码更难阅读。 See the last section of this answer for other ways to avoid the extra copy (assuming that you are inclined to worry about it).有关避免额外副本的其他方法,请参阅此答案的最后一部分(假设您倾向于担心)。


An alternative version of this function would actually translate the current object rather than returning a new one:此函数的另一种版本实际上会转换当前对象,而不是返回一个新对象:

translation2d& translation2d::translateBy(translation2d other)
{
    this->x += other.x;
    this->y += other.y;

    return *this;
}

Notice that I still return something, rather than void ;请注意,我仍然返回了一些东西,而不是void in this case I've followed the convention of returning a reference to the current object, which aids in chaining multiple modifications to the same object.在这种情况下,我遵循返回对当前对象的引用的约定,这有助于将多个修改链接到同一对象。 You'll find this pattern a lot in operator overloading.您会在运算符重载中发现很多这种模式。

Technically the this-> is unnecessary, but it makes the function a bit easier to understand.从技术上讲this->是不必要的,但它使函数更容易理解。


Note that 6502 changed the function to take other by const-reference, which is often a good idea and you should consider doing that too (though copying two double s is so cheap that it may not be worth the indirection).请注意,6502 将函数更改为通过常量引用获取other函数,这通常是一个好主意,您也应该考虑这样做(尽管复制两个double非常便宜,可能不值得间接使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM