1

以下のコードを実行すると、関数入力が呼び出されるまでにプライベート メンバー変数 (MaxRows、MaxCols) が変化することが示されます。何が起こっているのか助けていただけますか?

ご覧のとおり、最初のコンストラクターはプライベート変数の正しい表示を生成します。ただし、関数はそれらをバラバラにします。

#include <iostream>
#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>
using namespace std;

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

public:
    TwoD(int MaxRows, int MaxCols) 
    {
        outerArray = new double *[MaxRows];
        for (int i = 0; i < MaxRows; i++)
            outerArray[i] = new double[MaxCols];

            cout << MaxRows << MaxCols << endl;
    }

    void input()
    {
        cout << MaxRows << MaxCols << endl;
        for (int k = 0; k < MaxRows; k++)
        for (int j = 0; j < MaxCols; j++)
            cin >> outerArray[k][j];
    }

    void outPut()
    {
    for (int l = 0; l < MaxRows; l++)
    {
        for (int m = 0; m < MaxCols; m++)
            cout << outerArray[l][m] << " ";
        cout << endl;
    }
    }

    ~TwoD()
    {
    for (int i = 0; i < MaxRows; i++)
        delete[] outerArray[i];
    delete[] outerArray;
    }

};

int main()
{
TwoD example(5, 2);
example.input();
example.outPut();

return 0;
}
4

2 に答える 2

4

パラメータとして渡された値にクラス メンバを実際に設定することはありません。

また、命名規則を確認することをお勧めします - 通常はmaxRowsnotを使用しますMaxRows

TwoD(int maxRows, int maxCols) : MaxRows(maxRows), MaxCols(maxCols)
    {
        outerArray = new ...
于 2013-09-09T00:25:50.997 に答える