"Coupling
Finally, let's see how the use of non-member functions affects object coupling. It is a good design practice to promote loose coupling between objects. The less objects know about each other, the better. On the other hand, objects need to interact in order to make system functional. To solve this problem, Mediator Design Pattern has been introduced. Mediators are classes that handle interaction between different types and thus promote loose coupling. However, there are cases when a non-member function is enough to serve the role of a mediator. For instance, assume we have a class sample_db that wraps a database engine:
Code: Select all
class sample_db {...};
Code: Select all
class sample_db
{
...
public:
void write_sample_string (const sample_string& str);
};
Code: Select all
void write_sample_string_to_db (const sample_string& str, sample_db& db);
from:
http://www.codeproject.com/gen/design/nonmemberfnoo.asp
As a general rule, I don't like funny assignment operators, like,
Code: Select all
class gif;
class jpeg
{
....
jpeg( gif const & );
jpeg& operator=( gif const & );
....
};
Code: Select all
image_transforms.h
void operator=( jpeg &; gif const & );
Code: Select all
template <typename U, typename V>
U& operator=( U& u, V const & v )
{
u = v;
assert( u == v );
};