【问题标题】:Return object from class member function in C++从 C++ 中的类成员函数返回对象
【发布时间】:2016-01-28 10:58:15
【问题描述】:

任务是在上面的代码中编写计算新点的成员函数,即另外两个点的数量。而且我不知道如何返回对象或我应该做什么。这里是代码,函数标有三个!!!。函数必须返回一些东西,我不能让它为 void,因为对 void 的引用是不允许的。

    class point {
    private:
        float x;
        float y;
    public:
        point();
        point(float xcoord, float ycoord);
        void print();
        float dist(point p1, point p2);
    !!! float &add(point p1, point p2);
        float  &X();
        float &Y();
        ~point();
    };
    float & point::X() { return x; }
    float & point::Y() { return y; }
    point::point() {
        cout << "Creating POINT (0,0)" << endl;
        x = y = 0.0;
    }
    point::point(float xcoord, float ycoord) {
        cout << "Creating POINt (" << xcoord << "," << ycoord << ")" << endl;
        x = xcoord;
        y = ycoord;
    }
    void point::print() {
        cout << "POINT (" << x << "," << y << ")";
    }
    float point::dist(point p1, point p2) {
        return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
    }
   !!!// float & point::add(point p1, point p2) {
        point z;
        z.X() = p1.X() + p2.X();
        z.Y() = p1.Y() + p2.Y();
        z.print();
    }
    point::~point() {
        cout << "Deleting ";
        print();
        cout << endl;
    }
    int main() {
        point a(3, 4), b(10, 4);
        cout << "Distance between"; a.print();
        cout << " and "; b.print();
        cout << " is " << a.dist(a, b) << endl;
    }

我成功了!这是必须添加的功能

//prototype

    point &add(point& p1, point& p2);
//function itself

    point & point::add(point& p1, point& p2) {
        point z;
        z.x = p1.X() + p2.X();
        z.y = p1.Y() + p2.Y();
        z.print();
        return z;
    }

非常感谢 ForceBru!和你们所有人

【问题讨论】:

  • 如果你添加两个point,你很可能想要返回一个point,不是吗?
  • 不清楚你的问题是什么,如果你想从成员函数中返回一个对象,就这样做吧....
  • 并且顺便说一句,返回对私有成员的引用会破坏封装,这样您也可以将成员公开。我建议要么从一开始就将它们公开,要么从 getter 中返回值
  • @tobi303 是对的,尝试声明点 add(point x, pointy);然后在方法中result = new point();....返回结果
  • 不要返回对 add 函数内的对象的引用。

标签: c++ function object


【解决方案1】:

做什么

您也可以返回point

point point::add(const point& p1, const point& p2) {
    point result;

    result.x = p1.x + p2.x;
    result.y = p1.y + p2.y;

    return result;
}

请注意,这里不需要使用X()Y() 函数,因为此方法已经可以访问私有成员。

也可以进行运算符重载

/*friend*/ point operator+ (const point& one, const point& two) {
    // the code is the same
}

如何使用

int main() {
    point one(2,5), two(3,6), three;

    three.add(one, two);

    std::cout << "Added " << one.print() << " and " << two.print();
    std::cout << " and got " << three.print() << std::endl;

    return 0;
}

编辑:正如 cmets 中所说,您不应该返回对在您的 add 函数中创建的对象的引用,因为在这种情况下,您' 只允许返回对类成员和static 变量的引用。

【讨论】:

  • 你可以提到他绝不应该返回对result的引用。在他的代码中,这似乎是他的方法
  • 也许您可以通过扩展答案来显示他将如何在调用代码中使用它来提供帮助。即 OP 的最后 5 行
【解决方案2】:

您可以在这里使用运算符重载:

point point::operator+(const point & obj)  {

point obj3;
obj3.x = this->x + obj.x;
return obj3;

}

返回两个点相加的对象。

举个简单的例子:

class Addition  {
int a;
public:
      void SetValue(int x);
      int GetValue();
      Addition operator+(const Addition & obj1);
};
void Addition::SetValue(int x)
{
    a = x;
}
int Addition::GetValue()
{
   return a;
 }
Addition Addition::operator+(const Addition &obj1)
{
  Addition obj3;
  obj3.a = this->a + obj1.a;
  return obj3;
 }

int _tmain(int argc, _TCHAR* argv[])
{
  Addition obj1;
  int Temp;
  std::cout<<"Enter Value for First Object : "<<std::endl;
  std::cin>>Temp;

  obj1.SetValue(Temp);

  Addition obj2;
  std::cout<<"Enter Value for Second Object : "<<std::endl;
  std::cin>>Temp;

  obj2.SetValue(Temp);

  Addition obj3;
  obj3 = obj1 + obj2;
  std::cout<<"Addition of point is "<<obj3.GetValue()<<std::endl;

  return 0;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多