Circular reference

M

MRe

Hi,

This has been driving me crazy for the last few days, hopefully
someone can enlighten me?

As an example of what I'm trying to do, take the following class
structure (memory management not included, maybe take it to be more
pseudo code)

class TreeBranch
{
int AddItem() { (*items)[++nextItem] = new TreeItem(); return
nextItem; }
TreeItem * GetItem(int Index) { return (*items)[Index]; }
TreeItem * * items;
}
class TreeItem
{
int AddBranch() { (*branches)[++nextBranch] = new TreeBranch();
return nextBranch; }
TreeCollection * GetBranch(int Index) { return (*branches)
[Index]; }
TreeCollection * * branches;
}

How can I make this work, as both classes need to allocate the other
(this is the crux of the problem, everything else here is fluff)? I've
been trying to reorganise the code to remove the circular reference
with interfaces and class factories but I just cannot figure out how
to do it

Anyone any suggestions?

Thank you kindly,
Regards,
Eliott
 
L

Lionel B

Hi,

This has been driving me crazy for the last few days, hopefully
someone can enlighten me?

As an example of what I'm trying to do, take the following class
structure (memory management not included, maybe take it to be more
pseudo code)

class TreeBranch
{
int AddItem() { (*items)[++nextItem] = new TreeItem(); return
nextItem; }
TreeItem * GetItem(int Index) { return (*items)[Index]; } TreeItem *
* items;
}
class TreeItem
{
int AddBranch() { (*branches)[++nextBranch] = new TreeBranch();
return nextBranch; }
TreeCollection * GetBranch(int Index) { return (*branches)
[Index]; }
TreeCollection * * branches;
}

How can I make this work, as both classes need to allocate the other
(this is the crux of the problem, everything else here is fluff)? I've
been trying to reorganise the code to remove the circular reference with
interfaces and class factories but I just cannot figure out how to do it

The FAQ has some tips on this, which may or may not help you (difficult
to tell without a better idea of what you want to do). Have a look at:

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11

and the following two FAQs.
 
V

Victor Bazarov

MRe said:
As an example of what I'm trying to do, take the following class
structure (memory management not included, maybe take it to be more
pseudo code)
[..]
How can I make this work, as both classes need to allocate the other
(this is the crux of the problem, everything else here is fluff)? [..]

Pull the implementation of the functions that need to allocate memory
_out_ of their respective classes and define them _separately_. If
you decide to keep them in the headers, they need to _follow_ both
class definitions and in addition be declared 'inline'.

V
 
M

MRe

MRe said:
 As an example of what I'm trying to do, take the following class
structure (memory management not included, maybe take it to be more
pseudo code)
[..]
 How can I make this work, as both classes need to allocate the other
(this is the crux of the problem, everything else here is fluff)? [..]

Pull the implementation of the functions that need to allocate memory
_out_ of their respective classes and define them _separately_.  If
you decide to keep them in the headers, they need to _follow_ both
class definitions and in addition be declared 'inline'.

V

Of course! It's that easy. (I can't believe I spent so much time on
this, ugh)

Thank you very much,
Kind regards,
Eliott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top