c++ - 如何使用 operator+ 重载函数正确返回对象?

标签 c++

我创建了一个可以添加两个多项式系数的类,它们只是 一个动态数组索引位置。我的主要问题是 overload+ 函数返回值。一旦我添加 同一类的两个对象,如果我尝试返回一个对象,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;
}

最佳答案

我只是提出一些建议。 多项式copy ctor 有错误。应该是:

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

重载 vector 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;
}

加法运算符不需要是 friend ;它可以是:

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

但如果您必须作为 friend 实现它:

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
}

最后,我认为您应该为您的类实现一个移动构造函数 和一个移动赋值运算符

关于c++ - 如何使用 operator+ 重载函数正确返回对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59447244/

相关文章:

C++ : get week number from yyyyMMdd

c++ - 读取结构时出现未处理的异常

c++ - 在 C++ 中复制中小型内存块的最快方法

python - 相当于 python 的 set.pop() 用于 C++ 的无序集

c++ - 根据用户输入的天数计算生日

c++ - 在ubuntu上设置boost库的问题

c++ - 如何在 Vector 中存储类指针?

c++ - 如何在 Linux 和 C++ 上设计 GUI

C++删除节点二叉搜索树

C++、数组声明、模板、链接器错误