K
KaiWen
Many forward reference in the code. Is it a ugly design?
Many forward reference in the code. Is it a ugly design?
If it is then so is mine. I've got about 30
classes forward referenced. Can't get a clean
compile without doing that. This is a user
interface program with about 700 classes.
Lynn
If it is then so is mine. I've got about 30
classes forward referenced. Can't get a clean
compile without doing that. This is a user
interface program with about 700 classes.
I think a class reference many classes, it mean it know too much.
On 14.08.2011 05:42, Lynn McGuire wrote:
So, I think, the OP's question has too little information to say
anything (we don't know the reason for the forward references)
It's called a "forward declaration", by the way.
A large number of forward references need not be a sign of bad design.
For example, for a sufficiently large system build time can be an
important criterion. Trading some idealism for shorter build time can
make sense.
However, a large number of circular references indicate that things
could be simplified a lot. Whether it's worth the time to do it, that's
another question.
So, I think, the OP's question has too little information to say
anything (we don't know the reason for the forward references), and your
reply has too little information (we don't know the cost of working out
the circular references, or where they came from).
But as a general rule, it's a good idea to try to avoid circular type
dependencies.
Circular dependencies make things complicated both for physical
packaging of the code, and for understanding the constraints on data
structures etc., and for automating things via smart-pointers.
Cheers,
- Alf
Sorry, my english is not good. I'm a new hand. I use forward references to
break circular dependencies. It's just a smell when I wrote my class. I
felt it's not good when there are too many forward references. As you
said, I need a general rule. Is there any way (or pattern, if there is,
too happy) to avoid this.
Too many circular dependencies tends to indicate a lack of modularity,
every component needing to know the details of every other component.
One way to eliminate such dependencies is by splitting a class into an
interface (with no member variables and only pure virtual methods) and an
implementation (which derives from the interface).
E.g. rather than A having a reference to B and B having a reference to A,
A has a reference to X, B is derived from X, B has a reference to A. These
can be defined in the order X, A, B, with no circularity.
I use forward references to break circular dependencies.
It's just a smell when I wrote my class. I felt it's not good when
there are too many forward references.
As you said, I need a general rule. Is there any way (or pattern, if
there is, too happy) to avoid this.
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.