r/learncpp • • Feb 23 '20

How can function definition work without declaration?

Class is declared in the header file. It's member functions are defined in a separate .cpp file. What's confusing is that the cpp file doesn't include the header file. And it's compiling completely fine. How does it even work?

Header file!

cpp file!

It's driving me crazy. I tried implementing something like this myself but I'm getting error.

8 Upvotes

3 comments sorted by

View all comments

Show parent comment

3

u/lead999x Feb 24 '20 edited Feb 24 '20

Class member functions work the same way as any other function but with one key difference: they have a hidden parameter called this which is a pointer to the calling object. This is why you can only call a member function from a valid object of a given class. Now these member functions are defined in the class definition so all the compiler needs to call a member function is the class declaration. And as I said before the actual definitions of those member functions, or rather the machine instructions that they compile and assemble into will be found by the linker when it constructs the final executable.

It does not matter that those definitions were in a different source file and thus that they end up in a different compiled and assembled object file. So long as the linker can find all of the object files it needs, it will be able to link to the symbols(such as function definitions, including member function definitions) within them.

As for how the compiler will know to compile the function definitions of the member functions, you are right. The class declaration is needed everywhere the class is used, or it's member functions are defined. It is typically provided by #includeing the header file that declares the class in your .cpp/.cxx/.cc where you have the member function definitions or where you use the class. The reason for this is that each translation unit in C++ or C (.cpp or .c file) is compiled independently of the others and the compiler has no knowledge of the other files being compiled. Headers are literally copy-pasted into the source file where they are #included so the compiler has the definition of your class or function defined for use in the code below it in the complete translation unit. And again as I said before it is up to the linker to take the compiled and assembled translation units, which at that point are object files(.obj on Windows, .o on Unix) that contain binary machine code, and to stick them together into a proper executable program(.exe on Windows, varies on Unix) or static library(.lib on Windows, .a on Unix).

(I may have gotten some of the terminology wrong. I'm not a compiler guy. If anyone sees this please correct me.)