-3

Please find below some usage of C++ template. I am not able to fully understand these from syntactical and semantically point of view, e.g., First this is declared, which I know :

template <class T>
class Queue {// some other statements};

Then this is declared, which I understood partially, need to know what does it mean from syntactical and semantically point of view :

template <class T>
class IntermittentQueue : Queue<T> {// some other statements};

And finally this statements, which I again didn't understand fully

template <class T>
typename IntermittentQueue<T>::Node* IntermittentQueue<T>::getNode(const node_ptr nodePtr) {// some other statements };
linardni
  • 3
  • 3
  • 1
    IntermittentQueue is a class template derived from the class template Queue. And getNode is a member of that class template which returns a pointer to the Node member of that class template. – P.W Jan 16 '19 at 06:03
  • 1
    templates provide instructions for creating classes that differ by the template arguments. For instance Queue, Queue. They can be inherited and instantiate other classes and components where "T" might be different. – doug Jan 16 '19 at 06:08
  • @doug Thanks for your response. I am aware of this fact. I am more interested in knowing the other two usage. Out of the other two I can make out partially but not fully because if the internal is not known to me then I might have difficulty in instantiating these templates. So my main focus is on the other two. – linardni Jan 16 '19 at 07:25
  • @P.W Thanks for your clarification but I would like to know the usage of **typename** before _IntermittentQueue_. – linardni Jan 16 '19 at 07:30
  • @linard_in: Please see this post which has a detailed answer explaining this: https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords – P.W Jan 16 '19 at 07:34
  • @P.W Thanks for this link and as well as for your quick response. – linardni Jan 16 '19 at 07:41

1 Answers1

0
template <class T>
class IntermittentQueue : Queue<T> { /* some other statements */ };

This defines a new template class:

template <class T>
class IntermittentQueue { }

that at the same time inherits from another one, such as:

class Base { };
class Derived : Base { };

Solely, that in this case, the base class is an instantiation of another template class:

template <typename T>
class Base { };
class Derived : Base<int> { };

using the derived class' template parameter as template argument (and now we are back at the original code again...).

template <class T>
typename IntermittentQueue<T>::Node* IntermittentQueue<T>::getNode(const node_ptr nodePtr)
{ /* some other statements */ };

This is the implementation of one of the member functions of a template class. Step by step:

template <class T> // the class we are speaking off is a template class

typename IntermittentQueue<T>::Node* // return value of the function

IntermittentQueue<T>::getNode // function name with (template) class scope specified
                              // (just as with SomeClass::someMemberFunction)

 (const node_ptr nodePtr)         // function parameter(list)
 { /* some other statements */ }; // function body

Fine so far, but the return type might need further explanation:

typename IntermittentQueue<T>::Node*

The function returns a pointer to an object of inner class type Node of (template) class IntermittentQueue:

IntermittentQueue<T>::Node*

As inner type is a dependent type, you need to explicitly tell the compiler that this actually is a type at all, this is what the typename keyword then is used for; for details: there's already another question to the topic.

Just a side note here: Having typedefs for pointers (node_ptr) is bad practice, that's just information hiding and does not serve anything valuable for (the exception from: the pointer serves as a handle to some internal resource and is not intended to be dereferenced anywhere outside – then explicitly hiding the pointer nature is valid).

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • Thanks for your nice explanation, it helped me in understanding **typename** before _IntermittentQueue_ usage. Thanks again. – linardni Jan 16 '19 at 07:40