FCI-Helwan blog

Just another FCI-H weblog

Advanced C++ part 9 : Templates Part 1

I’ve decided to jump off a little and talk about templates. So we can dive into the world of STL and the wonders it can do.

As a kick-start, you can think of templates as a search-and-replace facility. No, it is not like preprocessor macros. Preprocessor macros does that at a syntactical level. But templates does it on a semantical level.

What’s the difference between a class and an object of that class ? The class defines how all the set of objects act, and what data they have. A single class might have numerous objects. Similarly is the difference between class template and template class !

A class template to a template class is as a class to an object. A class template defines how the template class look like. A class template might have several template classes.

As an example, consider the class Stack. It’s a simple stack with pop() and push() operations, an int array, and a top-of-stack counter members. The class is not concerned what is in the int array or what’s in the top-of-stack pointer, it merely states that they exist and how they are modified and handled.

Now consider the class template Stack<T>. “It’s a simple stack with pop() and push() operations, a T array, and a top-of-stack counter members”. “The class template is not concerned what is the actual type of the array”. It just states that we need an array of it, and that array will be treated that way.

The template class Stack<int> is typically like the normal stack we mentioned earlier. Note that Stack<int> and Stack<double> is totally different types. There code is written in separate places in the exe file. There is no relation between them whatsoever. Note that’s this is the a very strong difference between templates and generics in Java or C#. Generics just uses the same code that handles Object and casts the types back and forth to the type specified in the generic definition. Since this is not the case in templates, templates can also take variable parameters.
For example, you might define a Stack<int,5> that -for example- allows only 5 items in the stack. Templates also have default parameters, if you might use Stack<> it might default to int.

We’ve been talking about using templates, we still didn’t talk about defining them. That’s the next article isA.

(P.S. Don’t worry at all if you didn’t get anything from this article, it will get clearer as you read and see the examples in the next articles isA. It’s totally normal)

Advertisements

August 25, 2007 - Posted by | AdvancedC++

1 Comment »

  1. thanks alot,Nabil and iam waiting part || .. so plz make it quickly 🙂

    Comment by Mohamed Gamal El-Den | August 25, 2007 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: