Saturday, June 16, 2007

prefer non member non friend functions to member functions

if the function is just gathering functionality provided by other member functions which are actually interacting with class members etc... than you can consider making it a function in same namespace.

 

the more member functions or friend functions more code is possibly affected if you change the class implementation is changed.

 

this approach also reduces compilation issues since clients which use only one convenience function don't have to be recompiled on addition of another convenience function to namespace etc.

 

c++standard library is also organized the same way . their isnt a #include<c++standardlibrary> but #include <vector><lit> .....

 

partitioning functionality in this way is not possible when it comes from a class's member functions because a class must be defined in its entirety it cant be split into pieces

this also allows clients to add convenience functions if they want.

things to remember:

prefer non member non friend functions to member functions. doing so increases encapsulation, packaging flexibility and functional extensibility.

No comments: