0

In visual studio it is normal that the definition of the method will be in the header File.

when we need to write just the declaration in the header file, and write its definition in the implementation file (in its cpp file)? is any benefit to do that?

Lee
  • 142,018
  • 20
  • 234
  • 287
  • Imagine what happens when your function contains a static local variable and you include the header file into two separate compilation units. – David Heffernan Jul 12 '13 at 16:18
  • See also http://stackoverflow.com/q/1001639/96780 and, in particular, [my answer](http://stackoverflow.com/a/1001749/96780) quoting an article by Pedro Guerreiro. – Daniel Daranas Jul 12 '13 at 16:21

1 Answers1

0

One big advantage is that you don't have to recompile every file that uses that header if you change the implementation of the function. In fact, that's the way C and C++ were designed to be used. As your projects get larger you'll appreciate the benefits of decoupling interface (in the header) from implementation (in the .cpp file). (However, if you're writing templates this does not apply)

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • but is there any advantages when i separate some function into declaration and implementation (declaration of these functions in header file, and the implementation in cpp file), and the others functions just in the header file? i asked that because i have a programm in visual studio and just one function is separted into declaratio part in the header file, and the definition in the cpp file. but the others function are only in the header file. i dont know why and what is the benefit for this case? – joni klaoud Jul 12 '13 at 22:50