在类函数调用期间丢失私有变量信息(losing private variable information during the class function calls)

编程入门 行业动态 更新时间:2024-10-27 20:30:31
在类函数调用期间丢失私有变量信息(losing private variable information during the class function calls)

下面的代码将在运行时显示私有成员变量(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; }

Below codes will show, when run, that the private member variables (MaxRows, MaxCols) are changing by the time function input is called. Could you please help what is going on?

As you can see the first constructor generates correct display of private variables. However the function will break them apart.

#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; }

最满意答案

您实际上从未将类成员设置为作为参数传递的值。

另外:您可能希望查看您的命名约定 - 通常使用maxRows而不是MaxRows

TwoD(int maxRows, int maxCols) : MaxRows(maxRows), MaxCols(maxCols) { outerArray = new ...

You never actually set the class members to the values passed as parameters.

Also: you might want to look at your naming convention - normally use maxRows not MaxRows

TwoD(int maxRows, int maxCols) : MaxRows(maxRows), MaxCols(maxCols) { outerArray = new ...

更多推荐

本文发布于:2023-07-24 13:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1246287.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   函数   信息   losing   private

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!