0

I have to write a code containing 2 classes and one of them depend on the other Please see the image(just to simplify) if you can help me?

I cannot put the image

Mohd
  • 1
  • 2
    Please post code (formatted by indenting it with 4 spaces, or highlighting it and clicking the `{}` button), not images. If one class depends on another, you'll need to declare (and possibly define) the the class it depends on before using it. – Mike Seymour Mar 02 '15 at 17:11
  • @stefan I don't read this as (necessarily) a circular dependency. – crashmstr Mar 02 '15 at 17:30
  • @crashmstr Not necessarily, I agree. But either this is a non-issue, or it's circular dependency. – stefan Mar 02 '15 at 17:31
  • @stefan since we don't have enough information, this should be closed as such instead of as a duplicate (unclear vs. clear enough to know it is definitely a duplicate of that specific item). – crashmstr Mar 02 '15 at 18:13

1 Answers1

1

In c++ you cannot create two classes A and B, where A will have member variable of B and B will have member variable of A. This is because the sizes must be known in time of parsing the member by the compiler. If you think about it, this structure will be infinite size:

A will have B will have A will have B will have A ....

That is why it cannot be done.

You can only have pointers as variables. This is because, pointer is in the end, just memory address and nothing more, and memory address is always known (4, or 8 bytes).

A will have B* and that is all.

B will have A* and that is all.

To achieve that you can do forward declaration for example.

Community
  • 1
  • 1
Milan
  • 141
  • 6
  • Not entirely true. First of all, it also works with references (although in practice that may not be a good idea). Second, it is not at all guaranteed that pointers have 4 or 8 bytes. – Christian Hackl Mar 02 '15 at 17:34
  • I believe `A` can also contain a ***reference*** to `B`, and `B` a reference to `A`? (Very similar to a pointer, but not quite) – abelenky Mar 02 '15 at 17:34
  • @abelenky: Exactly. Although for member variables, a pointer is usually preferable. – Christian Hackl Mar 02 '15 at 17:35
  • @abelenky I don't think circular references are possible, since it's illegal in C++ to reseat a reference after it is created. (given that, how do you get the first-created object's reference to point to the second-created object, as the second-created object doesn't exist at the time you initialize the first reference? Well, I suppose you could do it by allocating child objects to contain the references, but that would be awkward) – Jeremy Friesner Mar 02 '15 at 17:37
  • @JeremyFriesner: I just wrote a Little Program that demonstrated that circular references are possible. – abelenky Mar 02 '15 at 18:12
  • `struct A; struct B { B(A& a_ref) : m_a(a_ref) { } A& m_a; }; struct A { A() : m_b(*(new B(*this))) { } B& m_b; }; int main(void) { A a; }` – abelenky Mar 02 '15 at 18:16
  • @abelenky cool... is there any way to do it without a dynamic allocation? – Jeremy Friesner Mar 02 '15 at 21:45