6

I was reading the topic Generic Classes .I got stuck at one point .Following is the piece of code

template <class StackType> class stack
{
    StackType stck[10];
    int tos;
public:
    stack() {tos=0;}
    void push(StackType ob);
    StackType pop();
};

My question is that in template <class StackType> class stack, there are two classes made (StackType and stack)?

I mean how does the compiler treat this as like making stack a nested class of StackType or something else?

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
Freedom911
  • 610
  • 2
  • 12
  • 26
  • 1
    StackType in this case is referring to the kinds of things your stack will contain. So if you create a stack, you will have a stack of integers. – Steve Howard Sep 20 '13 at 19:35

5 Answers5

10

It is not creating a second class, it is creating one class of type Stack where StackType is being supplanted by a user-given type. A better way to define this is to use typename (§14.1.2), which makes it more clear what is actually going on. For more information on templates, see C++ Templates.

What you are describing is nested classes in templates:

template <typename Type>
class Stack
{
public:
    class InnerType
    {
    };
};

In this example, InnerType is a class whose strong name is Stack<Type>::InnerType, which is a specific class based on the provided type. So if somewhere in code an object is created as follows:

auto x = new Stack<int>();

then a class of type Stack<int>::InnerType will exist. Template classes only exist in the assembly when they are used in code, so if no objects of any variation of Stack are implemented, the InnerType (of any template type) will not exist.

Also further detail about declaring templates is discussed here on SO.

Community
  • 1
  • 1
Will Custode
  • 4,576
  • 3
  • 26
  • 51
  • ok I got it but instead of stack() {tos=0;} we should have initialized StackType() {tos=0;}.shouldn't we? – Freedom911 Sep 20 '13 at 19:43
  • 1
    No, StackType is just the data type for the array stck. The field tos is part of the Stack<> class, so your example initializes it correctly. – Will Custode Sep 20 '13 at 19:45
  • 1
    Why do you use `new` in your example? – Oberon Sep 20 '13 at 19:56
  • 1
    Well this could definitely be exemplified using a stack variable (such as `Stack a( );`) but it's kind of convention when demonstrating constructors to use heap allocation (new). – Will Custode Sep 20 '13 at 19:59
4

There is no second class. StackType is the template argument. You can think of it just as you think about the argument to a function with one argument. Unless called and provided its argument, the function cannot do anything. The same with the template class StackType, it is the type argument to the template type stack. When you provide a type[name] as argument to the template class you specialize the template class. All this argument providing and specialization happens at compilation time. For instance, the first time when the compiler meets something like

stack< int > stackVariable;

or

typedef stack< int > StackOfInts;

only then does it actually compiles and instatiates a type named stack< int > - a stack of integers. Until that moment there is no stack of anything.

After instantiation with int your class becomes:

template<>
class stack< int >
{
  int stck[10];
  int tos;
public:
  stack() {tos=0;}
  void push(int ob);
  int pop();
};
CristiArg
  • 103
  • 2
  • 7
3

The class inside the <> is a different meaning than outside. It's essentially the same thing as typename.

Max
  • 21,123
  • 5
  • 49
  • 71
2

So a template is not a class. It is a way for a template to make a class from a user-defined type, e.g., int. So when I say:

stack<int>

The compiler generates a class stack < int > from your class, essentially filling in StackType with int and compiling that class.

So if I make a:

stack<stack<int> >

The compiler will generate two classes stact < int > and stack < stack < int > >

pippin1289
  • 4,861
  • 2
  • 22
  • 37
1

Templates create a family of classes - each of them having the same functionality but operate on different types.

In your example it is creating a family of classes. e.g. Stack of class X, Stack of class Y etc.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127