Suppose I have 2 header files, 1 .ipp extension file and a main.cpp file:
First header file(like interface in Java):
template<class T>
class myClass1{
public:
virtual int size() = 0;
};
second header file:
#include "myClass1.h"
template<class T>
class myClass2 : public myClass1<T>
public:
{
virtual int size();
private:
int numItems;
};
#include "myClass2.ipp"
And then is my myClass2.ipp file:
template <class T>
int myClass2<T>::size()
{
return numItems;
}
Last one is my main:
#include "myclass2.h"
void tester()
{
myClass2<int> ForTesting;
if(ForTesting.size() == 0)
{
//......
}
else
{
//.....
}
}
int main(){
tester();
return 0;
}
myClass1, myClass2 and myClass2.ipp belong to header file. main.cpp in source file. What's the advantages by using this way to implement your program instead of using just .h and .cpp files? And what is .ipp extension file? The difference between .ipp and .cpp?
C++ source code file that contains inline functions for a program; wraps functions with a macro that can include or exclude the functions when running a program; used to turn off inlining functions during the debugging process, and turn them back on again for production code.
There is no difference. They're exactly the same.
cxx is just an alternative to . cpp used by some compilers. . hpp is an attempt to distinguish header files where there are significant c and c++ differences.
h files, or header files, are used to list the publicly accessible instance variables and methods in the class declaration. .cpp files, or implementation files, are used to actually implement those methods and use those instance variables.
TR;DR
The .cpp
file is a separate translation unit, the .ipp
is included from the header and goes into all translation units including that header.
Explanation
Before templates, you put the declarations of methods in the header file and the implementation went to a .cpp
file. These files were compiled separately as their own compilation unit.
With templates, this is no longer possible almost all template methods need to be defined in the header. To separated them at least on a logical level, some people put the declarations in the header but move all implementations of template methods to .ipp
files (i
for "inline") and include the .ipp
file at the end of the header.
One more advantage I see in using .ipp files is that you can choose whether to include the implementation part of the templates. This allows you to reduce the compilation time by instantiating your templates for some parameters in a .cpp file, such that they are pre-compiled, while keeping the possibility to instantiate the templates for other parameters. Example:
// x.hpp
template <typename T>
struct X
{
int f();
}
// x.ipp
#include "x.hpp"
template <typename T>
int X::f()
{
return 42;
}
// x.cpp
#include "x.ipp"
// Explicit instantiation of X<> for int and double;
// the code for X<int> and X<double> will be generated here.
template class X<int>;
template class x<double>;
// foo.cpp
// Compilation time is reduced because
// the definitions of X member functions are not parsed.
#include "x.hpp"
void foo()
{
X<int> x;
x.f();
}
// bar.cpp
// Here we need to include the .ipp file because we need to instantiate
// X<> for a type which is not explicitly instantiated in x.cpp.
#include "x.ipp"
#include <string>
void bar()
{
X<std::string> x;
x.f();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With