PDA

View Full Version : C++ Templates


aunabidi
02-07-2006, 04:41 AM
Hello. I have an assignment in which I have to write a template with 3 functions:
void Add();
void Remove();
void Print();
What I dont get is that the teacher asked us to define the functions outside the template class body. Where do I define the functions then in the .h file or .cpp file? And what does defining a function mean? Help would be greatly appreciated. Thanks a lot!

oracleguy
02-07-2006, 07:09 PM
What compiler are you using? Visual Studio/Microsoft C++?

Ender
02-13-2006, 01:03 AM
Note: I use gnu g++ for my compiler.

If this is going to be a reusable interface, you would do it in a header file.
As for the code... it would look something like this:


/* The class name T registers a certain class that will be used with this list
* i.e.
* List<string> list;
* In Java this concept is called Generics which is a useful way to
* think about it, since it makes classes more generic and reusable
*/
template <class T>
class List
{
public:
// interface here (functions add(), remove(), and print()
void add(T);
T remove(int);
private:
/* object members, i.e. head and tail node pointers for a linked list
* or an array if you're going to do it via growable arrays */
};
// Yes, there's a semi-colon here... it comes after all data structures with
// brackets, like classes, structs, enums, etc.

/* You'll notice that the functions are outside of the class
* In C++, functions are often declared inside the class and defined outside of it.
* Java does it differently,
* with everything being declared and defined inside the class
******************************************
* The List:: reference just means that the function is a member of the List
* class. You have to define all concrete functions that you declare.
* And this reference gives proof to the compiler.
******************************************
* If you define the function outside of the class
* You are required to template the function
******************************************
* You are requred to parametrize List so that the compiler knows
* The function is using the same T parameter as the class
*/
template <class T>
void List<T>::add(T element)
{
// implementation
}

/* The function can take a Template type as a parameter
* As well as return it.
* It can use it as it could any other member
*/
template <class T>
T List<T>::remove(int index)
{
// implementation
}

/* You can also use templates for functions alone, apart from classes
* Here is an example (note that I don't use the List:: reference)
*/
template <class T>
void store(T element)
{
// implementation
}

/* Here are examples of calling the functions
* and instantiating a List
*/
int main(int argc, char* argv[])
{
// Using the templated List class
List<string> list;
list.add("hello world");
string s = list.remove(0);
// Using the non-class-member templated function
store<int>(1337);
// Note: this would cause the compiler to cough out an error
store<int>("Don't do this, just an example");
return 0;
}


To sum it up, templates allow your code to be more generic. You can register a type, which can be an object or a primitive, when declaring an instance of a class or calling a function. By templating your class, the classes members can use that type registered when declaring the instance of the class, but must include the template <class whatever> and you must include ClassReference<Whatever>:: when defining a function of a class.