官术网_书友最值得收藏!

Learning about templates

Another technique to add to your toolbox of programming concepts that we will use in the next section is the idea of templates. Templates are a way for you to be able to create generic classes that can be extended to have the same functionality for different datatypes. It's another form of abstraction, letting you define a base set of behavior for a class without knowing what type of data will be used on it. If you've used the STL before, you've already been using templates, perhaps without knowing it. That's why the list class can contain any kind of object.

Here's an example of a simple templated class:

#include <iostream> // std::cout 

template <class T>
class TemplateExample
{
public:
// Constructor
TemplateExample();
// Destructor
~TemplateExample();
// Function
T TemplatedFunction(T);
};

In this case, we created our TemplateExample class and it has three functions. The constructor and deconstructor look normal, but then I have this TemplateFunction function which takes in an object of type T, and returns an object of type T. This T comes from the first line of our example code with the template <class T> section of our code. Anywhere that there is a T it will be replaced with whatever class we want to use this template with.

Now, unlike regular functions, we have to define templated functions within our .h file, so that, when we need to create an object using this template, it will know what the functions will do. In addition to this, the syntax is also a bit different:

template <class T> TemplateExample<T>::TemplateExample() 
{
printf("\nConstructor!");
}

template <class T> TemplateExample<T>::~TemplateExample()
{
printf("\nDeconstructor!");
}

template <class T> T TemplateExample<T>::TemplatedFunction(T obj)
{
std::cout << "\nValue: " << obj;
return obj;
}

In this example, I'm just printing out text to display when a certain functionality is called, but I also want to point out the usage of std::cout and that using it will require you to add #include <iostream> to the top of your file.

We are using the standard library's cout function in this instance, instead of the printf that we have been using, because cout allows us to feed in obj--no matter what its type is--to display something, which isn't possible with printf by default.

Once that's finished, we can go ahead and use this inside of our project:

  TemplateExample<int> teInt; 
teInt.TemplatedFunction(5);


TemplateExample<float> teFloat;
teFloat.TemplatedFunction(2.5);

TemplateExample<std::string> teString;
teString.TemplatedFunction("Testing");

As you can see, this will create three different kinds of TemplateExample class objects using different types. When we call the TemplatedFunction function, it will print out exactly the way we were hoping:

Later on, when we learn about abstract types, we can use templates with them to handle any kind of data. In our case right now, we are going to use this functionality to allow us to make as many Singletons as we'd like!

主站蜘蛛池模板: 桦甸市| 湘阴县| 那曲县| 余干县| 惠州市| 从化市| 冕宁县| 北宁市| 五常市| 敦煌市| 内江市| 治县。| 贵溪市| 乌兰察布市| 武城县| 鄂尔多斯市| 深水埗区| 措美县| 武夷山市| 页游| 贵阳市| 汝城县| 巍山| 巴彦淖尔市| 武城县| 南阳市| 五家渠市| 穆棱市| 新昌县| 遵义市| 琼结县| 读书| 巴彦淖尔市| 武城县| 台山市| 城口县| 永和县| 五河县| 廊坊市| 五原县| 兴和县|