首页 > 解决方案 > 如何使用运算符+重载函数正确返回对象?

问题描述

我创建了一个可以添加两个多项式系数的类,它们只是动态数组索引位置中的数字。我的主要问题是重载+函数返回值。一旦我添加了同一个类的两个对象,如果我尝试返回一个对象变量,Visal Studio 会给我一个错误,但是如果我将构造函数返回给一个对象,那么代码可以正常工作,并且没有错误。我不知道为什么会出现这个错误,就像我之前对动态数组的分配一样,我返回对象变量没有任何问题?在这种情况下,我不确定 operator+ 重载函数的返回值规则是什么?谢谢。

#include <iostream>
#include <vector>
using namespace std;


class polynomial
{
public:
  polynomial();
  polynomial(vector<double> vec);
  polynomial(const polynomial& obj);
  void get();
  polynomial& operator=(const polynomial& rightSide);
  friend const polynomial operator +(const polynomial& x, const polynomial& y);
  //The function in question
  ~polynomial() { delete[] p; };
private:
  double *p;
  int expSize;
};


int main() 
{
  vector<double> x(3);
  vector<double>y(3);
  x = { 2,3,4 };
  y = { 4,3,2 };

  polynomial X(x);              //<-- Both objects are constructed with vectors
  polynomial Y(y);

  polynomial obj;
  obj = X + Y;                 //<-- Adding dynamic arrays, and saving the values in obj.

  obj.get();
  cout << endl;

  system("pause");
  return 0;
}

polynomial::polynomial()
{
  expSize = 3;
  p = new double[expSize];

  for (int c = 0; c < expSize; c++)
      p[c] = 0;
}

polynomial::polynomial(vector<double>vec)
{   
  expSize = vec.size();

  p = new double[expSize];
  for (int c = 0; c < expSize; c++)                 
      p[c] = 0;                                     
  for (int c = 0; c < expSize; c++)                 
      p[c] = vec[c];                                

}

polynomial::polynomial(const polynomial& obj)
{
  p = new double[expSize];
  for (int c = 0; c < expSize; c++)
      p[c] = obj.p[c];
}

polynomial& polynomial::operator=(const polynomial& rightSide)
{
  if (this == &rightSide)
      return *this;
  else
  {
      expSize = rightSide.expSize;
      delete[] p;
      p = new double[expSize];

      for (int c = 0; c < expSize; c++)
          p[c] = rightSide.p[c];

      return *this;
  }
}

const polynomial operator +(const polynomial& x, const polynomial& y)
{
  polynomial obj;

  if (x.expSize > y.expSize)                //<-- obj.expSize private member variable will 
      obj.expSize = x.expSize;              //    inherit the larger dynamic array index size
  else if(x.expSize <= y.expSize)           //    of the two parameter objects.
      obj.expSize = y.expSize;

  for (int c = 0; c < obj.expSize; c++) {
      obj.p[c] = x.p[c] + y.p[c];
  }
  vector<double>vec(obj.expSize);            //<-- Vector will inherit the new index size too.
  for (int c = 0; c < obj.expSize; c++) {
      vec[c] = obj.p[c];                     //<-- Vector takes joined values of two objects
  }

  //return polynomial(vec);                 //<-- Returning a constructor with vector works fine
  return obj;                              //<-- abort() has been called error
}

void polynomial::get()
{
  for (int c = 0; c < expSize; c++)
      cout << p[c] << endl;
}

标签: c++

解决方案


我只是提出一些建议。的复制 ctorpolynomial一个错误。它应该是:

polynomial::polynomial(const polynomial &obj)
{
  expSize = obj.expSize;
  p = new double[expSize];
  for (int c = 0; c < expSize; c++) p[c] = obj.p[c];
}

重载向量ctor可以这样定义:

polynomial::polynomial(const vector<double> &vec)
{   
  expSize = vec.size();
  p = new double[expSize];
  for (int c = 0; c < expSize; c++) p[c] = vec[c];
}

赋值运算符可以这样定义:

polynomial& polynomial::operator=(const polynomial &rightSide)
{
  if (this != &rightSide)
  {
    //// if two polynomials have the same expSize then
    //// they should have an array of the same size
    if (expSize != rightSide.expSize)
    {
      expSize = rightSide.expSize;
      delete[] p;
      p = new double[expSize];
    }

    for (int c = 0; c < expSize; c++) p[c] = rightSide.p[c];
  }

  return *this;
}

加法运算符不需要是朋友;有可能:

class polynomial
{
  polynomial operator+(const polynomial &rightSide) const
  {
    //
  }
};

但是如果您必须将其作为朋友来实现:

polynomial operator+(const polynomial &x, const polynomial &y)
{
  vector<double> vec;
  for (int c = 0; ((c < x.expSize) || (c < y.expSize)); ++c)
  {
    vec.push_back(0);
    if (c < x.expSize) vec.back() += x.p[c];
    if (c < y.expSize) vec.back() += y.p[c];
  }
  //return polynomial(vec); OK
  return vec; // also OK
}

最后,我认为您应该为您的班级实现一个移动 ctor和一个移动赋值运算符


推荐阅读