0

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?

user2706035
  • 95
  • 1
  • 1
  • 6
  • It's not impossible. If you store via indirection in one or the other, you can achieve it with forward declarations, just like you'd resolve any other cyclic dependency. I'd ask yourself why you think you need it, though. – Lightness Races in Orbit Jan 12 '14 at 14:23

1 Answers1

1

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;
};
sliser
  • 1,645
  • 11
  • 15