Advertising
I understand that it is good programming practice to not have executable code in header files.For instance you may have a class definition with function prototypes in a .h file (I'll call it Crap.h) that looks like this:
- Code: Select all
class Crap{
int x;
int y;
public:
Crap();
int Add();
};
And a CPP file that contains the member function definitions (I'll call it Crap.cpp) that may look like:
- Code: Select all
Crap::Crap(){
x = 5;
y = 10;
}
int Crap::Add(){
return x + y;
}
My question is, why must ALL code be in the .h file if you have a templated class like this:
(Lets assume that the '+' operator has been overloaded for whatever 'T' becomes)
- Code: Select all
template <class T>
class Crap{
T x;
T y;
public:
Crap(T, T);
T Add();
}
template <class T>
Crap<T>::Crap(T one, T two){
x = one;
y = two;
}
template <class T>
T Crap<T>::Add(){
return x + y;
}
If someone could dumb it down retard style for me I will be your friend.