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?
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++.
A nested class is declared within the scope of another class. The name of a nested class is local to its enclosing class.
Inner classes Then, create the inner object within the outer object with this syntax: OuterClass. InnerClass innerObject = outerObject. new InnerClass();
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.
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();
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With