I am working with a book on c++ and to solve the problems I am always asked to declare the member functions’ prototypes in the xxxxxx.h file and define the functions body properly in the xxxxxx.cpp file. Is there any harm or disadvantage in defining the member functions in the .h file? If not, is there any benefit or advantage in defining them in the .cpp file?
Share
If you will always write your code in
.hfiles you will never be able to performa some technics, such asforward declaration. Since you can’t use forward declaration if writing code in headers you will be not able to solvecross dependencies.So you will need to include everything you need in
.hfile. Including this file in other file will include everything already included. Thus any small change in you code will result in almost full recompilation in many cases.Also it’s harder to read the code in
.hfiles because you expect to see the interface of the class in.h, not implementation.