1

I'm learning C ++ and I have a question about header files. Are they only for classes? If I have some functions and want to use them in several files, should I write the declaration in the h file and the definition in the cpp file? Thanks for the help.

jTymbark
  • 25
  • 1
  • 6
  • 3
    No, you can use them to declare functions, enums ... – drescherjm Apr 06 '22 at 22:19
  • 4
    ***should I write the declaration in the h file and the definition in the cpp file?*** Yes – drescherjm Apr 06 '22 at 22:19
  • 3
    Headers define an interface. That interface could be for just about anything. – user4581301 Apr 06 '22 at 22:21
  • 4
    Headers are used all the time in C which doesn't even *have* classes. – Mark Ransom Apr 06 '22 at 22:24
  • 1
    @SilvioMayolo: That's kinda backwards, it sounds restrictive when the actual rule is expansive. Headers are generally the place for all declarations (template or not). The important interaction is that for template functions, headers are the place for definitions in addition to declarations. – Ben Voigt Apr 06 '22 at 22:27
  • 1
    @BenVoigt Fair, I wrote that a bit confusingly. I'll just delete it and let your comment stand – Silvio Mayolo Apr 06 '22 at 22:29

0 Answers0