Including related files

K

Kristo

Richard said:
Hmm.

Friend's a kludge because it breaks encapsulation.

It's only a kluge if you use it incorrectly. See FAQ 14.2.
void* and casts are kludges because they break type safety.

Yep, that's why the C++ cast operators are so ugly. :)
But what does a forward declaration break?

You'll often see a forward-declared class used to solve the problem of
mutual inclusion, i.e., when two headers #include each other. The
forward declaration allows it to compile, but more often than not
you're looking at a design problem.

Kristo
 
R

Richard Herring

In message said:
It's only a kluge if you use it incorrectly. See FAQ 14.2.

Indeed. I was just trying to find a reason why it might be called a
kludge at all.
Yep, that's why the C++ cast operators are so ugly. :)


You'll often see a forward-declared class used to solve the problem of
mutual inclusion, i.e., when two headers #include each other. The
forward declaration allows it to compile, but more often than not
you're looking at a design problem.

At that design level, yes, there may in some cases (I think "more often
than not" is too strong) be a problem with mutual *inclusion*.

But it's much more frequently used to solve legitimate problems of
mutual *reference*, cases where objects of two mutually-dependent
classes genuinely need to contain pointers or references to each other,
but nothing more; perhaps the most obvious is when a child needs to know
who its parent is.

But that wasn't really my point. The forward declaration itself doesn't
break anything [*]; on the contrary, it conveys the minimum possible
information about a class, hiding all the unnecessary detail. I'd say
that was a good and useful thing to be able to do, and it's unfair to
give it the same derogatory label as tricks involving casting to void*.


[*] except (in a different sense of the word) mutual-inclusion loops!
 

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,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top