这个问题让我不太了解C++.我试图访问我放在堆中的特定值,但我不确定如何访问它.在我的问题中,我在一个对象中的数据成员函数中放置了一个值,我试图在另一个数据成员函数中访问它.问题是我不知道如何,我在网上搜索过例子,但没有一个是我需要的东西,因为它们都在int main()中并且不是我需要的具体内容.
在第一个数据成员函数中,我声明了我想要发送给Heap的值; 这是我的第一个数据成员函数的示例.
void Grid::HeapValues()
{
//Initializing Variable
value = 2; //The type is already declared
//Pointers point a type towards the Heap
int* pValue = new int;
//Initialize an a value of in the Heap
*pValue = value;
}
Run Code Online (Sandbox Code Playgroud)
并且在数据成员函数中这是想要的:
void Grid::AccessHeap()
{
//Extracting heap:
int heap_value = *pValue; //*pValue does not exist in this function
cout << heap_value; //Delays the value 2, which is found
//in the first data member function
}
Run Code Online (Sandbox Code Playgroud)
我觉得愚蠢的问题,但我无法找到答案,也不知道如何.有谁知道如何以简单的方式从堆中访问值?我需要它能够访问两个以上的数据成员函数.
abe*_*nky 11
pValue需要是Grid类的成员变量.
class Grid
{
private: int* pValue;
public: void HeapValues();
void AccessHeap();
};
Run Code Online (Sandbox Code Playgroud)
现在可以从Grid的任何成员函数访问成员变量pValue.
完成后,不要忘记在析构函数中删除指针.欲了解更多信息,请访