For my homework assignment, I have to create a linked list and it must be a template class. The assignment calls for one template class called List and another nested template class called Link.
The methods should be defined outside of the class definition.
I created a simple version of the assignment first and it’s working, but I feel there is an error in my design.
#include <iostream>
using namespace std;
template <class T1>
class A
{
public:
template <class T2>
class B;
T1 _a;
};
template <class T1>
template <class T2>
class A<T1>::B
{
public:
T2 _b;
};
int main()
{
A<int> a;
a._a = 5;
A<int>::B<int> b;
b._b = 10;
cout << a._a << " " << b._b << "\n";
return 0;
}
My main question is, since the nested class will be of the same type, as you can see from the statement A<int>::B<int> b;, is there a way to have class B automatically be of the same type as A? Does my question even make sense? In other words, can I do A<int>::B b; and b automatically the same type as A? There will never be a situation where I would have A<double>::B<int>; for example.
Yes, just make
Bnot a template.T1inB‘s definition still refers to the template argument ofA.Now, instead of
A<int>::B<int>, it’s simplyA<int>::B.List<int>::Link<double>wouldn’t make sense anyway, so there’s no point in it being a template.