0

I'm tasked with implementing a class. I have been provided with a .h file entitled "pro.h" that I cannot change in any way. The .h file only contains function prototypes, no definitions. After the class block, it has a

#include "pro.hpp"

I'm supposed to create a file.hpp to add the definitions although, I'm confused because definitions are usually included in a .cpp file not another header file. Why do you think this would be done? And would I format the definitions the same why I'd do it in a .cpp file, or rewrite the header file to include the definitions (which would seem inefficient).

1 Answers1

1

Your teacher wants you to implement a template class. The thing about templates is that they need to get instantiated with the correct template type, which means that you can't create the binary before you know what type its gonna be. That is why the implementation is usually written inside the header file. Have a look f.e. here...

If you are new to templates, just ask uncle google or ant wiki ;)

ADD:

To put it simply... when you have something in foo.cpp, it gets translated to binary and the corresponding header foo.hpp serves as a reference to what functions are there in the binary that I could use from my other code. The important thing is that this binary does not (really) change anymore.

On the other hand templates can't be transformed into the binary, until you know what type it is going to be operating on... If you use the template class once with int and another time with vector<double> for template parameter, the resulting binary could be very very different... Therefore you can only compile the code into the binary once you know the type, and therefore you need to pass along the code (inside the header) instead of just function prototypes...

Hope this is clear. It is after all almost 3 o'clock in the morning here.

If not, this should be very comprehensive.

Community
  • 1
  • 1
Kupto
  • 2,802
  • 2
  • 13
  • 16