Templates..

Hi everyone,

I have an code to write about 3 containes using templates(for different data types).

But I am having a hard time grasping the logic on how to create these containers.

do I declare three of them in main() or do I have to use miltipe classes

close to what I am looking for:

1
2
3
4
5
6
7
8
9
10
11
12
13
template <class T>
class Container
{
protected:
int size, capacity;
T* cont;

public:
Container ();
~Container();
Container(const Container& );
....
};


I do not know where I create 3 containers and store data in them do whatever with them

Any help is appreciated

Thanks in advance
there are alot approaches in template programing,
usualy you will place declaration AND definition as well into header file and include it into source file where your main() is or any other funciton is.
thanks for the reply codekiddy, but I still have a hard time understanding the concept
Last edited on
closed account (zb0S216C)
It's hard to understand what you mean, Detroit.

The whole idea of templates is to reduce the amount of code required to perform a specific action. For example, I have 2 function definitions, both of which perform the exact same action for 2 different types. Now, if I made the function a template, I would only have to write the function once, since the compiler uses the function definition as a template to follow.

Class Template and Template Class mean two different things. Class Template is a class which the compiler uses to generate code; that is, a template the compiler will follow when instantiating it. Template Class is an instantiation of a Class Template. When an instantiation is made, the compiler generates a instantiation of the class template based on the type specified. For example:

1
2
3
4
5
6
7
8
9
10
template <typename T>
class Class
{
    public: T X;
};

int main() 
{
    Class<int> F;
}

When the compiler encounters F, it will generate an instantiation of Class based on int. That instantiation is then compiled. Therefore, the above code translates to:

1
2
3
4
class Class
{
    public: int X;
};

Now, if you want your container to handle different types, such as reference, or pointers, you'll have to specialise your class to handle those types. The following code exemplifies this:

1
2
3
4
5
6
7
8
9
10
template <typename T>
class Class<T*>
{
    public: T *X;
};

int main()
{
    Class<int*> F;
}

When a class is specialised, the compiler will look through the definitions of the class and choose the best fit. Since Class was specialised, the compiler realised that you're going to treat a pointer to type T differently.

Further, this code shows the latter:

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
33
34
template <typename T>
class MyClass
{
    private:
        T X;

    public:
        T &GetX()
        {
            return(this->X);
        }
};

template <typename T>
class MyClass<T*>
{
    private:
        T *X;

    public:
        T &DerefAndReturnX()
        {
            return(*this->X);
        }
};

int main()
{
    MyClass<int> X;
    MyClass<int *> Y;
  
    X.GetX() = 10;
    Y.DerefAndReturnX() = &X.GetX();
}

See how both instantiations of MyClass have different contents? This is because we've handled pointers differently.

Wazzak
Last edited on
Topic archived. No new replies allowed.