4
I’m starting to learn C++
Object oriented, and I have to make an algorithm using chained lists. In C
, I used a structure that had as one of the attributes a pointer to the structure itself. I wonder if it is possible to do something similar to the C++
, but using classes instead of structures.
Follow an example of what I intend to do:
class item {
public:
item(int profit, item *next);
~item();
insertItem(int profit, item *ptList);
returnItem(int i, int w, int W, item *ptList);//Função que retorna o objeto de uma matriz simulada com lista encadeada
private:
int profit;//Valor correspondente ao objeto
item *next;//Ponteiro para o próximo item da lista
};
Are you having any problems? Not only is it possible, but chained lists are assembled anyway. Of course it needs some care not to create a dependency on creation that creates a loop infinite, but it’s rare to be able to do that.
– Maniero
Actually, I don’t know practically anything about C++, so my fear of doing so. My intuition said that was it, but I wanted to be sure. Thanks for the help!
– Eder Matheus