在 class 中使用私有指针来访问 C++ 中的数组

using pointers as private in class to access an array in c++

我正在使用一个 int 指针作为私有指针来访问一个数组。当我为存储和获取数组值编写单独的函数时,程序崩溃了。但是如果我在构造函数中编写获取值和存储值的代码,程序就可以正常工作。我找不到问题出在哪里。

程序 1:(不起作用)

#include<iostream>

using namespace std;

class NewArray{
private:
    int Size = 0;
    int *arrAddr = NULL;

public:
    NewArray(int);
    void SetValue(int);
    void GetValueOf(int);
};

//Array is created
NewArray::NewArray(int arSz){
    int arr[arSz];
    Size = arSz;
    arrAddr = arr;
    cout << "An array of Size " << Size << " is created" << endl;
}

// Store Value function
void NewArray::SetValue(int index)
{
    cin >> *(arrAddr+(index));
}

//Get value function
void NewArray::GetValueOf(int idx)
{
    if ((idx >= Size) || (idx < 0))
    {
       cout << "index value is out of bound" << endl;
    }
    else
    {
       cout << *(arrAddr+idx) << endl;
    }
}

int main()
{
    int arrSize, arrIdx;

    cout << "enter the size of array" << endl;
    cin >> arrSize;

    if (arrSize > 0)
    {
        NewArray ar(arrSize);

        cout << "enter " << arrSize << " values. Enter the values one after the other." << endl;
        for (int i = 0; i < arrSize; i++)
        {
            ar.SetValue(i);
            ar.GetValueOf(i);
        }

        cout << "enter the index to fetch the value" << endl;
        cin >> arrIdx;
        ar.GetValueOf(arrIdx);
    }
    else{
        cout << "invalid input" << endl;
    }
    return 0;
}

程序 2:(正在运行的代码)

#include<iostream>

using namespace std;
// size is passed

class NewArray{
private:
    int Size;
    int *arrAddr;

public:
    NewArray(int);
    void GetValueOf(int);
};

NewArray::NewArray(int arSz){
    int arr[arSz];
    int idx;
    Size = arSz;
    arrAddr = arr;
    cout << "An array of Size " << Size << " is created" << endl;

// Storing values in array
    cout << "enter " << Size << " values. Enter the values one after the other." << endl;
    for (int i = 0; i < Size; i++)
    {
        cin >> *(arrAddr+i);
    }

// To get the value from the index
    cout << "enter the index to fetch the value" << endl;
    cin >> idx;

    if ((idx >= Size) || (idx < 0))
    {
       cout << "index value is out of bound" << endl;
    }
    else
    {
       cout << "The value is " << *(arrAddr+idx) << endl;
    }
}


int main()
{
    int arrSize, arrIdx;

    cout << "enter the size of array" << endl;
    cin >> arrSize;

    if (arrSize > 0)
    {
        NewArray ar(arrSize);
    }

    else{
        cout << "invalid input" << endl;
    }

    return 0;
}

我已经尝试过这个特定的例子,程序 1 在数组大小为 10 并且我试图写入第 7 个索引时崩溃。

谁能帮我找出原因吗?

在构造函数中 NewArray::NewArray() 您创建一个数组,该数组存储在堆栈中。离开构造函数后,它的生命周期结束,它会从堆栈中删除,因此通过指针访问它 arrAddr 是未定义的行为。

要简单地解决问题,您需要使用 newdelete 在堆上分配数组或将其存储为 class 成员。

这只是两种实现方式。我推荐任何东西,它们只是可能性

忘记了 int *。使用 vector 代替。看here

#include <iostream>
#include <conio.h>
#include <vector>

using namespace std;

class Class1 {
private:
    vector<int> intArr;
public:
    void Set(int iValue) {
        intArr.push_back(iValue);
    }
    int Get(int iIndex) {
        return intArr[iIndex];
    }
};

int main() {
    Class1 class1;
    for (size_t i = 0; i < 10; i++)
    {
        class1.Set(i);
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << class1.Get(i) << endl;
    }
    _getch();
}

int arr[arSz];

在堆栈上分配,因此它的生命周期仅限于定义它的函数 - 一旦堆栈展开,分配的堆栈内存可供其他人使用。您需要使用 new 运算符在堆上分配内存,以便内存在函数调用后保持不变。

arrAddr = new int[arSz];

上面的代码在堆上分配内存并且在调用 delete [] arrAddr 明确删除之前可用,在大多数情况下应该只在析构函数中完成。