Advertising (This ad goes away for registered users. You can Login or Register)

Templated Class Question

Programming on your favorite platform, for your favorite platform? Post here
Post Reply
electrosheep
Posts: 97
Joined: Tue Jan 11, 2011 2:50 am

Templated Class Question

Post by electrosheep » Wed Nov 30, 2011 11:09 pm

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 ***.h) that looks like this:

Code: Select all

class ***{
  int x;
  int y;
public:
  ***();
  int Add();
};
And a CPP file that contains the member function definitions (I'll call it ***.cpp) that may look like:

Code: Select all

***::***(){
  x = 5;
  y = 10;
}

int ***::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 ***{
  T x;
  T y;
public:
  ***(T, T);
  T Add();
}

template <class T>
***<T>::***(T one, T two){
  x = one;
  y = two;
}

template <class T>
T ***<T>::Add(){
  return x + y;
}
If someone could dumb it down **** style for me I will be your friend.
Advertising
Let me see...your grandmother's name was Beatrice?

hardhat
Posts: 58
Joined: Tue Feb 08, 2011 6:10 pm

Re: Templated Class Question

Post by hardhat » Wed Dec 14, 2011 7:55 pm

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.
Advertising

Sirius
Posts: 103
Joined: Sat Dec 18, 2010 3:31 pm

Re: Templated Class Question

Post by Sirius » Tue Apr 24, 2012 5:59 am

You can separate the code doing something like this

***.h

Code: Select all

template <class T>
class ***{
  T x;
  T y;
public:
  ***(T, T);
  T Add();
};

#include "***.cpp"
***.cpp

Code: Select all

template <class T>
***<T>::***(T one, T two){
  x = one;
  y = two;
}

template <class T>
T ***<T>::Add(){
  return x + y;
}

Post Reply

Return to “Programming”