C
Cy Edmunds
Victor Bazarov said:Tom said:Victor said:Cy Edmunds wrote:
[..] In C++
there is no reason to break encapsulation just because you want to
add some functionality.
I've read that expression several times today, and still have not
understood how putting functionality that specific to any particular
class inside that class means "breaking encapsulation". Care to
elaborate?
If you add functionality to a class via (non-friend) non-members, then
that functionality necessarily only depends on the public interface of
the class.
Yes, and?... Where is the "breaking" part? If the functionality is
instead actually _part_ of public interface, what's broken? Or is adding
to a class' interface in fact "breaking" some fragile "encapsulation"?
What is the essential difference between
class A {
// some interface, public or otherwise
};
void foo(A& a) {
// some implemenation
}
and
class A {
// some interface, public or otherwise
static void foo(A& a) {
// same implemenation as above
}
};
What is broken in the second case compared to the first one?
V
In the second case a mistake in the implementation of foo can result in
violation of invariants of the class.