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– Isac
How so allocate each pointer manually? For each person I would have to allocar?
– cYeR
Something like
pessoas[0] = new Pessoas();
, which naturally applied with afor
. When I said pointer referred to object, allocate each Person of the array manually– Isac