How to use a class array as a pointer?

Asked

Viewed 221 times

2

How do I use a class array as a pointer? I tried several ways, but C++ didn’t let you compile by generating errors.

An example of class(It’s just one example, the class I need to use is thousands of times larger than this)

class Pessoas
{
private:
    std::string Nome;
public:
    std::string getNome() const { return this->Nome; }

    void setNome(std::string Nome) { this->Nome = Nome; }
};

Pessoas pessoas[1000]; // Forma atual que estou utilizando, compila corretamente, porém eu preciso que ela fique allocada no heap
Pessoas* pessoas = new Pessoas[1000]; // Compila corretamente, porém, eu tenho que utilizar o operador . para acessar os membros, o que me parece estranho pois geralmente eu utilizo o operador ->, dessa forma ela está sendo allocada no heap?

The second way she’s being allocada in the heap? If it’s not as I do to allocar in the heap?

An example of how I wanted to leave(Without overloading operators, as if it were a pointer):

pessoas[1]->setNome("Fulano");
pessoas[2]->setNome("Sicrano");

I’m trying to store the class in the heap because as the class is great I believe it will not fit in the stack, another reason is that I need the class data to stay until the end of extremely large functions which I believe does not happen in the stack.

  • From the syntax you have of example it seems to me that you are looking for an array of pointers, which would be Pessoa **pessoas. However this implies allocating each person pointer manually. Otherwise you can use as it has and use the syntax with ., at the end is the difference between having an array of people and an array of pointers for people

  • How so allocate each pointer manually? For each person I would have to allocar?

  • Something like pessoas[0] = new Pessoas();, which naturally applied with a for. When I said pointer referred to object, allocate each Person of the array manually

1 answer

2


In the first case it is in the stack and in the second in the heap. But in both cases the members should be accessed with the . and not with ->.

Only use -> to override a pointer. But you’ve done this by calling the index with the operator []. So what was left was an instance of Pessoas whose members you access with .

Ex:

#include <iostream>
#include <string>

using namespace std;

class Pessoas
{
    private:
        std::string Nome;

    public:
        std::string getNome() const { return this->Nome; }
        void setNome(std::string Nome) { this->Nome = Nome; }
};


int main()
{
    Pessoas pArray[1000];
    Pessoas* pessoas = new Pessoas[1000];

    pArray[0].setNome("Pessoa Array");
    pessoas[0].setNome("Pessoa Heap");

    cout << "!" << pArray[0].getNome() << endl;
    cout << "!" << pessoas[0].getNome() << endl;
}

I compiled with visual C++ 2015

The exit was left:

!Pessoa Array
!Pessoa Heap
  • Thank you, but why doesn’t the -> operator apply in this case since it is on the heap and is a pointer?

  • I edited the answer. See if it’s clear.

  • 1

    Yes, you were, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.