Pregunta

I know, the title is confusing. But i didn't know any other way to forumlate it. The Problem is as follows:

I have two classes. One is the Listclass and the other is the Itemclass. So the Listclass has a list (std::vector) of Itemobjects. But now I'd like to use a parent member in the itemclass, that refers to the Listclass (or correctly object) in which it is stored.

But C++ forbids something like that, because it only alows members of already parsed classes. I have mostly worked with Java in the past, where this was not a problem.

So is it even possible to do something like this?

¿Fue útil?

Solución

You need pointer or reference which can be used with incomplete types.

struct list; // <-- forward declaration

struct item
{
    item(list &list_) : _list(list_) {}

    list &_list;
};

struct list
{
    std::vector <item> items;
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top