我想編寫一個代表一組整數的類。這是一項家庭作業,但對於我來說,我無法弄清楚這個問題。C++:指針指向的值變化
在類「IntSet」中,我有兩個私有變量;一個是指向數組的指針,另一個指向數組的大小。我可以創建這個類的對象,並按預期工作。但是我有一個名爲「join」的函數,它返回IntSet類的一個對象。它基本上將數組連接在一起,然後使用該數組創建返回對象。
這裏是我的代碼:
#include <iostream>
using namespace std;
class IntSet {
int * arrPtr;
int arrSize;
public:
//Default Constructor
IntSet() {
int arr[0];
arrPtr = arr;
arrSize = 0;
}
//Overloaded Constructor
IntSet(int arr[], int size) {
arrPtr = arr;
arrSize = size;
}
//Copy Constructor
IntSet(const IntSet &i) {
arrPtr = i.arrPtr;
arrSize = i.arrSize;
}
/*
* Returns a pointer to the first
* element in the array
*/
int* getArr() {
return arrPtr;
}
int getSize() {
return arrSize;
}
IntSet join(IntSet &setAdd) {
//Make a new array
int temp[arrSize + setAdd.getSize()];
//Add the the values from the current instance's array pointer
//to the beginning of the temp array
for (int i = 0; i < arrSize; i++) {
temp[i] = *(arrPtr + i);
}
//Add the values from the passed in object's array pointer
//to the temp array but after the previously added values
for (int i = 0; i < setAdd.getSize(); i++) {
temp[i + arrSize] = *(setAdd.getArr() + i);
}
//Create a new instance that takes the temp array pointer and the
//size of the temp array
IntSet i(temp, arrSize + setAdd.getSize());
//Showing that the instance before it passes works as expected
cout << "In join function:" << endl;
for (int j = 0; j < i.getSize(); j++) {
cout << *(i.getArr() + j) << endl;
}
//Return the object
return i;
}
};
int main() {
//Make two arrays
int arr1[2] = {2 ,4};
int arr2[3] = {5, 2, 7};
//Make two objects normally
IntSet i(arr1, 2);
IntSet j(arr2, 3);
//This object has an "array" that has arr1 and arr2 concatenated, essentially
//I use the copy constructor here but the issue still occurs if I instead use
//Inset k = i.join(j);
IntSet k(i.join(j));
//Shows the error. It is not the same values as it was before it was returned
cout << "In main function:" << endl;
for (int l = 0; l < k.getSize(); l++) {
cout << *(k.getArr() + l) << endl;
}
return 0;
}
的程序編譯和輸出的是現在:
In join function:
2
4
5
2
7
In main function:
10
0
-2020743083
32737
-2017308032
我不知道爲什麼,但10和0都是一樣的每我重新編譯並運行。另外,如果我打印出指針的地址而不是值(在連接函數和主函數中),我會得到相同的內存地址。
對不起,如果我濫用術語,我來自java背景,所以指針等對我來說有點新鮮。如果需要澄清,請詢問。
在此先感謝。
在你的構造函數中聲明一個局部變量並保持一個指針不能很好 – codah