Please help with templates...

Here is what I understand about classes.

A class is a blueprint for objects made from the blueprint. Much like the USS George Washington is a Nimitz class aircraft carrier made in the likeness of the USS Nimitz using the blueprints from the Nimitz. The navy can build as many of them as they like and they are all essentially the same. Although each ship can be customized somewhat but still retain the identity of the ship for which it was made.

Each of these 'ships' are objects made from the original class. It is not the class which is the usable objects but the objects themselves. This is where the analogy breaks down in that the class, the Nimitz, was not only the original blueprint but was itself a usable object.

But what about class templates. The template allows you to create one basic class that can have different types based on the calling function and what that function's arguments send to the template. This results in one or many almost identical classes created to accommodate the different arguments and their types.

You might have a class whose internal types are ints, or doubles, or floats, etc. Here's where I start to not understand...

But at that point don't you only have several classes which are themselves blueprints to some future objects that will be created from them?

As I understand it the new class has to be instantiated to actually create it. At that point do we now have a blueprint for a future object? Or is the class itself an object?

For example, a function calls the class that has a template. The template makes the substitutions and we instantiate the class. Now I have a int class. Do I now make objects from that templated class? Or is this class itself the object?

I could use some help

tex
Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <map>

template <typename Resource>
class ResourceManager
{
public:
	const Resource* LoadResource( const std::string _id);

private:
	std::map<std::string, Resource*> m_resources;
};

class Mesh
{
public:
};

class Material
{
public:
};

int main()
{
	ResourceManager<Mesh> meshManager;
	ResourceManager<Material> materialManager;

	meshManager.LoadResource("Mesh1");
	materialManager.LoadResource("Material1");

	return 0;
}


ResourceManager is just a generic class which can be instantiated with whatever template parameter you pass it.

EDIT:
Also to answer some of your questions which I somehow skipped doing :P

Basically as you already stated, a class is a blueprint for objects. A class template on the other hand is more of a blueprint for other classes. The compiler creates a class for you with the specified template parameters. That class then gets instantiated during run-time. So pretty much it's a two-phase thing. The compiler creates a class based on template parameters and then that created class is instantiated.
Last edited on
Thank you.

Let's talk about the class. Isn't it true that any class, regardless of how its created, whether via an inheritance process where it is built up from one or more parent classes, or by way of a template...is just a class. In other words, at the end of the creating process you are left with a class from which other objects will be made. Stated another way, regardless of how the class is created, you end up with a class that, by itself, does nothing but provide a blueprint for an future created object.

But, it seems from my studies that a template created class can actually act as the object that does stuff. This has me confused.

tex
But, it seems from my studies that a template created class can actually act as the object that does stuff. This has me confused.
Can you show an example of this, so we could explaing what happens.

Class templates are exactly that: templates, or blueprints, for creating classes, or types.

There is no type vector: vector is a template, not a class. On the other hand vector<int> denotes a type: it can be treated as any other type, be it funcdamental type of user-defined struct.

Classes contain blueprints for objects (data members), function for object manumulation (function members) and some other stuff (like static data members).


You cannot use non-instantiated template, as it is just a blueprint. As soon as you instantiate a template, it bacomes a normal class, so you can do same things like with other classes, including static member access:
1
2
3
4
5
6
//Here we access member type alias value_type 
//from type std::vector<int> which is instantiated template std::vector
std::vector<int>::value_type foo; 

//Following is an error as std::vector is not a class, but a template
std::vector::value_type
@OP

I generally like your analogy of the blueprints of the aircraft carrier. As you alluded, your analogy did break down when you tried to state that Nimitz was the original blueprint. Actually, the set of original blueprints from which the Nimitz were built is the class. The Nimitz is an object of the class, not the class itself.

Now, suppose that the Nimitz-class aircraft carriers had interchangeable engines (yeah, this is absurd, but bear with me). One ship would have a nuclear engine, while another would have a diesel engine and yet another would have a "thetexan" engine. Now there's no such thing as a single set of blueprints for a Nimitz class air craft carrier. There are now 3 sets of blueprints--1 that contains a nuclear engine, 1 that contains a diesel engine, and 1 that contains a "thetexan" engine. Everything else about the sets of blueprints is identical, but none of the sets of blueprints is complete without an engine module.

In this case, the set of blueprints is a template class where the type of engine is the template parameter. You need to provide the type of engine (template parameter) before you can generate the set of blueprints (class) before you can build an aircraft carrier (object).
Topic archived. No new replies allowed.