Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct template for nested class C++

I can't figure out the correct syntax to write a correct template for a nested class. I'd like to do something like this

template <typename T>
class list {
private:
    class node {
    public:
        T value;
        node();
        ~node();
    };

public:
    node<T> *H;
    list();
    ~list();
};

I want to have a class to represent each element of the outer class, so I'd like to have the inner class to be hidden inside the outer. Is this possible? Or should I use a different approach?

like image 276
fedemengo Avatar asked Aug 29 '17 12:08

fedemengo


People also ask

What is a class template in C?

Templates Specialization is defined as a mechanism that allows any programmer to use types as parameters for a class or a function. A function/class defined using the template is called a generic function/class, and the ability to use and create generic functions/classes is one of the critical features of C++.

What is nested class in C?

A nested class is declared within the scope of another class. The name of a nested class is local to its enclosing class.

What is the syntax of nested class?

Inner classes Then, create the inner object within the outer object with this syntax: OuterClass. InnerClass innerObject = outerObject. new InnerClass();

What is the syntax of template class?

1. What is the syntax of class template? Explanation: Syntax involves template keyword followed by list of parameters in angular brackets and then class declaration.


1 Answers

You don't need to specify the template parameter for the inner class (because it isn't declared as template class):

template <typename T>
class list {
private:

    class node {
    public:
        T value;
        node();
        ~node();
    };

public:
    node *H; // <<<<<<
    list();
    ~list();
};
like image 151
user0042 Avatar answered Oct 19 '22 23:10

user0042