Random Homebrew: Vector Infector

Templated Class Question

Programming on your favorite platform, for your favorite platform? Post here

Templated Class Question

Postby electrosheep » Wed Nov 30, 2011 11:09 pm

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.
Let me see...your grandmother's name was Beatrice?
electrosheep
 
Posts: 169
Joined: Tue Jan 11, 2011 2:50 am

Re: Templated Class Question

Postby hardhat » Wed Dec 14, 2011 7:55 pm

Advertising
In the first case, all of the code of your functions will be compiled once, and linked in once. The result is that the executable will be a little smaller, if that class is used in many files. On the otherhand, certain things need to be optimized, such as get/set methods perhaps. Those can be marked inline, and then the compiler will attempt to inline the functionality, instead of adding the overhead of calling a separate function.

In the second case, you have a template class type, and the compiler won't know until the template is used, what type to generate the class methods for ahead of time. As a result, the template has to have all of the code in the header, and it is resolved based on the type requested in C++.

There are other languages that implement that same thing in different ways. In Java it resolves it at run time. In Ada, the linker actually recompiles the templated functions (in Ada it is called Generics) with the appropriate data type substituted in. But in C++ it is the task of the compiler, and it has to do it at compile time when referenced by another module.
hardhat
 
Posts: 58
Joined: Tue Feb 08, 2011 6:10 pm

Re: Templated Class Question

Postby Sirius » Tue Apr 24, 2012 5:59 am

You can separate the code doing something like this

Crap.h
Code: Select all
template <class T>
class Crap{
  T x;
  T y;
public:
  Crap(T, T);
  T Add();
};

#include "Crap.cpp"

Crap.cpp
Code: Select all
template <class T>
Crap<T>::Crap(T one, T two){
  x = one;
  y = two;
}

template <class T>
T Crap<T>::Add(){
  return x + y;
}
Sirius
 
Posts: 128
Joined: Sat Dec 18, 2010 3:31 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

Friends

Coding 'n Cracking - Nymphaea - PS3 Forum - darkforestgroup - daxhordes.org - Tgames - coldbird - gopsp.it - pspstation.org - prometheus - hgoel.info - MakeSmartTV - ps vita