这个问题涉及到我对 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;
}
而在数据成员函数中这是想要的:
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
}
我觉得问很愚蠢,但我找不到答案,也不知道怎么做。有谁知道如何以简单的方式从堆中访问一个值?而且我需要它能够访问两个以上的数据成员函数。