The most common valuable use of the member functions that I encounter in everyday life is to reduce code complexity by providing one template function instead of many functions that do pretty much the same thing.
For example, suppose you are working on a server that receives half a dozen related messages and stores incoming data in half a dozen tables in a database. The direct implementation will be the implementation of 6 message processing functions (psudocode):
class MessageProcessor { void OnMessage(const char* msg); void ProcessMessage100(Data100* data); void ProcessMessage101(Data101* data); void ProcessMessage102(Data102* data); void ProcessMessage103(Data103* data); void ProcessMessage104(Data104* data); void ProcessMessage105(Data105* data); }; MessageProcessor::OnMessage(const char* msg) { unsigned int * msgType = ((unsigned int*)msg); switch( *msgType ) { case 100 : ProcessMessage100((Data100*),sg); break; case 101 : ProcessMessage101((Data101*),sg); break; :: } } MessageProcessor::ProcessMessage100(Data100* data) { Record100* record = GetRecord100(key); record->SetValue(xyz); } MessageProcessor::ProcessMessage101(Data101* data) { Record101* record = GetRecord101(key); record->SetValue(xyz); } : :
There is an opportunity to generalize ProcessMessage () functions, since they do essentially the same thing:
class MessageProcessor { OnMessage(const char* msg); template<class Record, class Data> void Process(Data* data); }; template<class Record, class Data> void MessageProcessor::Process<Record,Data>(Data* data) { Record* record = GetRecord(key); record->SetValue(xyz); }
The GetRecord function can also be generalized, which gives a code base with two functions, where it used to be 12. This improves the code due to its simplicity with fewer moving parts, it is easier to understand and maintain.
source share