重载运算符保持变量更新而不是保留原始变量

时间:2013-09-14 09:37:08

标签: c++ arrays class operator-overloading

我正在尝试使用数组进行一些算术运算。如数组[1,2,3] + [1,2,3]应返回[2,4,6]。但是当我试着做下一个算术[1,2,3] * [1,2,3] =它返回[4,16,36]而不是我想要的[1,4,9]。在我看来,原始数组已永久更新为第一次算术运算的结果。请帮助比概念支持更深入,例如为我提供一些替代编码建议,因为我正在学习C ++,这是我遇到的最棘手的章节。非常感谢你。

#include <iostream>
using namespace std;
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <windows.h>
#include <cstring>
#include <cctype>
#include <iomanip>
#include <algorithm>
#include<sstream>


class TwoD
{
private:
    int MaxCols;
    double* outerArray;
    double constant;

public:
    TwoD(int size)
    {
        MaxCols = size;
        outerArray = new double[MaxCols];
    }
    TwoD(int size, double constantInput)
    {
        MaxCols = size;
        outerArray = new double[MaxCols];
        for (int k = 0; k < size; k++)
            outerArray[k] = constantInput;
    }
    TwoD(const TwoD& other): MaxCols(other.MaxCols)
    {
        outerArray = new double[MaxCols];

        for (int i = 0; i < MaxCols; i++)
            outerArray[i] = other.outerArray[i];
    }
    const TwoD& operator =(const TwoD& rightSide)
    {
        if(MaxCols != rightSide.MaxCols)
        {
            delete [] outerArray;
            outerArray = new double [rightSide.MaxCols];
        }
        MaxCols = rightSide.MaxCols;

        for (int i = 0; i < MaxCols; i++)
            outerArray[i] = rightSide.outerArray[i];

        return *this;
    }
    void input(int size)
    {
        for (int k = 0; k < size; k++)
                cin >> outerArray[k];
    }
    void outPut(int size)
    {
        for (int l = 0; l < size; l++)
        {
            cout << outerArray[l]<< ", ";
        }
        cout << endl;
    }
    const TwoD operator + (const TwoD& rightSide)
    {

        for (int l = 0; l < MaxCols; l++)
        {
            outerArray[l] = outerArray[l] + rightSide.outerArray[l];
            cout << endl;
        }

        return *this;
    }
    const TwoD operator * (const TwoD& rightSide)
    {
        for (int l = 0; l < MaxCols; l++)
        {
            outerArray[l] = outerArray[l] * rightSide.outerArray[l];
            cout << endl;
        }

        return *this;
    }
    ~TwoD()
    {
        delete[] outerArray;
    }
};

int main()
{
    int size;
    double constantInput;
    cout << "Please enter a size of an array" << endl;
    cin >> size;

    TwoD example1(size);
    cout << "input example1" << endl;
    example1.input(size);
    example1.outPut(size);

    cout << "Please enter a constant to work with: "<< endl;
    cin >> constantInput;
    TwoD constant1(size, constantInput);
    constant1.outPut(size);

    cout << "polynomial + polynomial" << endl;
    TwoD result1 = example1 + example1;
    result1.outPut(size);

    cout << "polynomial * polynomial" << endl;
    TwoD result2 = example1 * example1;
    result2.outPut(size);


    return 0;
}

1 个答案:

答案 0 :(得分:2)

您需要停止修改原始值:

const TwoD operator + (const TwoD& rightSide) const
//                                   add this ^^^^^
{
    // create a new object suitable to hold the result
    TwoD result(MaxCols);
    for (int l = 0; l < MaxCols; l++)
    {
        // set the values in the result
        result.outerArray[l] = outerArray[l] + rightSide.outerArray[l];
        cout << endl;
    }
    // and finally return it
    return result;
}