0

The .cpp file includes it's .h file in order to define the things declared in the .h file, and a different .cpp file includes that .h file to use whatever classes/functions it has. But at no point do the two .cpp files communicate with each other directly, so how does the latter .cpp file know how the classes and functions are defined, if the only thing they see are the declarations (and not the definitions)?

Also, is it bad practice to define things in a .h file or declare things in a .cpp file?

James Ronald
  • 685
  • 1
  • 6
  • 13
  • "Also, is it bad practice to define things in a .h file or declare things in a .cpp file?" - Not necessarily. – eesiraed Jun 10 '19 at 19:16
  • 2
    All latter cpp file needs to know are the declarations . That's sufficient for the compiler to generate code for that cpp file. This is called seperate compilation. It's the linkers job to bring everything together. Only it needs to know the definition of everything. Good question BTW. – john Jun 10 '19 at 19:19
  • You may want to use .hpp or .hxx for C++ header files. Makes a difference when you mix languages or use C language libraries. There are C++ language elements that can go into a C++ header file but can't be compiled by a C compiler. – Thomas Matthews Jun 10 '19 at 19:56
  • 1
    Reminder: When defining variables in header files, every source file that includes the header file gets a copy of the variables. Same with constant literals. – Thomas Matthews Jun 10 '19 at 19:57
  • Header files containing templates may have code in the templates, and thus there is code in the header file. – Thomas Matthews Jun 10 '19 at 19:58

0 Answers0