c++ - Which is best for a repeating piece of code? -


I have a class with two member functions that share a part of the code:

 
Code> Zero A :: first () {first function epilog (); SharedPart (); } Zero A: second () {secondfunction epigl (); SharedPart (); }

Currently First Function Epilog () , secondFunctionEpilogue () and Shared Part () calls But not just code fragments, shared part () code is being repeated. I want to get rid of duplication.

The shared piece of code does not require access to any member of the class. Therefore, I can apply it in any of three ways:

  • A stable member function,
  • a CONST non-static member function or
  • < Li> A Local Function

Which version is better and why?

If your function reaches the state but it does not change, then use the consonant member function.

Your case:

If this does not require your function 1) access to any member of the code, and 2) that belongs to that class , Then make it a stable work of your class.

In this way it is clear that it is not modifying the state nor the position of the object.

An additional case you have not mentioned:

There is one more thing that you can do too. And that is, your Shaidepart takes a member function pointer and calls it and then processes its main body. If you have first (), second (), third (), fourth (), ... such functions, then there may be less code duplication, thus you do not need to share SharePart (); At the end of each member function, and you can reuse the first (), seconds (), third () ... without calling the sharedpart () code.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -