On Jan 28, 3:52 am, "Grizlyk" <grizl...@yandex.ru> wrote:
> Grizlyk wrote:I need to point self class in template parameter, for example
> template <
> class Tptr,
> class Tc_container
> >
> class Viterator
> {
> public:
> virtual char next(const char is_check_bound=0);
>
> Tc_container *container;
>
> virtual ~Viterator(){}
>
> };
>
> class Tptr;
> Viterator< Tptr, Vcontainer<Tptr,"Viterator"> > a;
>
> How can we point to itself here: "Viterator" (after class "Viterator" has
> been declared)?
There is no class named Viterator - only a class template named
Viterator. And a class template is not a class - nor even a type.
Now it is possible to create classes from the Viterator class
template. Classes created in this way are named VIterator<Type1,
Type2> where Type1 and Type2 are the names of the parameterized types
used to instantiate the Viterator class template.
> We need only name of class in the case. It can be easy done for non-template
> class
>
> class X
> {
> public:
> X* next; //I can do it
>
> };--
Viterator can do the same:
template <class T1, class T2>
class Viterator
{
public:
Viterator * next;
...
};
But the actual request in this case is entirely different.
Essentially, the aim is to instantiate the class template,
Viterator,with two types: T1 and T2, and have T2 be the type:
Viterator<T1, T2>. In other words T2 in Viterator<T1, T2> is supposed
to be none other than Viterator<T1, T2> itself.
Well, it would be a neat trick if it could be pulled off, but sadly,
it's not possible in C++ to instantiate a class template with the
complete, instantiated type as one of its own type parameters. In
other words, in order to instantaite Viterator in this case, the
program must first instantiate Viterator, but before that can be done,
the program must first instantiate Viterator - and so on and so forth.
So even if this kind of self-referential instantiation were allowed in
C++ - the infinite recursion involved would translate into a long wait
for the program to finish compiling.
Greg
|