C++ 访问堆

发布于 2024-07-11 06:52:30 字数 881 浏览 9 评论 0原文

这个问题涉及到我对C++了解不够。 我正在尝试访问放置在堆中的特定值,但我不确定如何访问它。 在我的问题中,我已将对象中数据成员函数的值放入堆中,并且我尝试在另一个数据成员函数中访问它。 问题是我不知道怎么做,我在网上搜索了示例,但没有一个是我需要的,因为它们都在 int main() 中,而不是我具体需要的。

在第一个数据成员函数中,我声明了要发送到堆的值; 这是我的第一个数据成员函数的示例。

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
}

我觉得问这个问题很愚蠢,但我无法找到答案,也不知道如何找到答案。 有谁知道如何以简单的方式从堆中访问值? 我需要它能够访问两个以上的数据成员函数。

This problem involved me not knowing enough of C++. I am trying to access a specific value that I had placed in the Heap, but I'm unsure of how to access it. In my problem, I had placed a value in a heap from a data member function in an object, and I am trying to access it in another data member function. Problem is I do not know how, and I had searched examples online, but none were what I needed as they were all in int main() and were not specifically what I needed.

In the first data member function, I declare the value I want to be sent to the Heap;
Here's an example of what my first data member function.

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

And in data member function This is what want:

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
}

I feel foolish for asking, but I am unable to find the answers and do not how. Does anyone know how to access a value from the heap in a simple way? And I would need it to be able to access in more then two data member function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世界和平 2024-07-18 06:52:30

pValue 需要是 Grid 类的成员变量。

class Grid
{
  private: int* pValue;
  public: void HeapValues();
          void AccessHeap();
};

现在可以从 Grid 的任何成员函数访问成员变量 pValue。

pValue needs to be a member-variable of the class Grid.

class Grid
{
  private: int* pValue;
  public: void HeapValues();
          void AccessHeap();
};

Now the member-variable pValue is accessible from any member-function of Grid.

此岸叶落 2024-07-18 06:52:30

完成后,不要忘记在析构函数中删除指针。 欲了解更多信息,请访问:

Don't forget to delete your pointer in the destructor when you are done. For more information visit:

宛菡 2024-07-18 06:52:30

就像 Aaron 所说,您可以使该值成为 Grid 类的成员。 但在这种情况下,它不需要是指向 int 的指针。

class Grid
{
private:

    int value;

public:

    void HeapValue();
    void AccessHeap();
};

无论实例化该值,该值都将作为对象的一部分存储。 你可以把它放在栈上或者堆上,这并不重要。 对于简单的值,如内置类型和对象,将由类的实例拥有,没有必要使用 new 来分配它们。 这样,您就不必担心使用 Grid 析构函数中的删除运算符进行清理,只需确保正确处理拥有的 Grid 实例即可;-)

当然,也有一些例外情况,您将在深入研究时了解这些例外情况到 C++,但对于你的例子,上面的就可以了。

Like Aaron said you can make the value a member of your Grid class. In this case though there is no need for it to be a pointer to an int.

class Grid
{
private:

    int value;

public:

    void HeapValue();
    void AccessHeap();
};

The value will be stored as part of the object wherever it is instanciated. You can make it on the stack or the heap, it doesn't matter. For simple values like the built in types and Objects that will be owned by the instance of the class it is unnecessary to allocate them using new. This way you don't need to worry about cleaning up with the delete operator in the Grid destructor, just make sure you dispose of the owning Grid instance properly ;-)

Of coarse there are exceptions to this that you will learn as you delve more into C++, but for your example the above will be fine.

梦中的蝴蝶 2024-07-18 06:52:30

为什么要把它放在堆上? 如果将其添加为类的一部分,那么它将位于类所在的同一位置,可能在堆栈上或全局内存中。 也许您希望整数指针具有可变大小? 在这种情况下,您需要确保在使用完内存后释放它。

堆上的东西的问题是找到它。 除非您为此添加一种机制,否则无法通过名称访问它。 您需要以某种方式将位置传达给需要访问它的任何代码。 在本例中,看起来您只需要在 Grid 类中进行访问,因此很容易。 只需将其设为 Aaron 所示的成员变量即可。 你可能会得到类似这样的结果:(

class Grid
{
protected:
    int* pVals;
public:
    Grid() pVals(NULL) { }
    ~Grid() { delete [] pVals; }
    void HeapValues() {
        pVals = new int[getHeapValuesSize()];
        pVals[0] = 1; // ...
    }
    void AccessHeap() {
        cout << pVals[0]; // ...
    }

顺便说一句,当你的意思是“成员函数”时,你似乎使用了短语“数据成员函数”。“数据成员”指的是类的成员数据,如 pVals,但是我不确定“数据成员函数”是什么意思。)

Why do you want it on the heap? If you add it as part of the class then it will be in the same place the class is, possibly on the stack or in global memory. Perhaps you want to have a variable size to your integer pointer? In that case, then you need to be sure to deallocate the memory when you are done with it.

The problem with stuff on the heap is finding it. There is no accessing it by name, unless you add a mechanism for that. Somehow you need to communicate the location to whatever code needs to access it. In this case, it looks like you only need access within the Grid class, so it is easy. Just make it a member variable like Aaron indicates. You might end up with something like:

class Grid
{
protected:
    int* pVals;
public:
    Grid() pVals(NULL) { }
    ~Grid() { delete [] pVals; }
    void HeapValues() {
        pVals = new int[getHeapValuesSize()];
        pVals[0] = 1; // ...
    }
    void AccessHeap() {
        cout << pVals[0]; // ...
    }

(On a side note, you appear to be using the phrase "data member function" when you mean "member function". "Data member" refers to member data of a class, like pVals, but I'm not sure what "data member function" would mean.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文