Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have the template class' pointer as a parameter when declaring an object of that class' type in C++?

Tags:

c++

templates

Here is the code-version of the question:

   template <class T>
   class Foo
   {
       //Stuff
   }
   Foo<Foo*> object;

Compiling this gives me:

"a template argument 1 is invalid error."

My question is,

  • if it is possible to do the intended action above and if so, how and why does that said method work?
  • If it isn't possible, I'm guessing it's because of having a circular reference inside the template parameter itself (e.g. Foo<Foo<Foo<.....>>> but please let me know the correct reason if I'm incorrect in my thinking.

Also I tried a forward declaration of Foo, and that did not work.

like image 514
Enis Avatar asked Dec 06 '25 05:12

Enis


1 Answers

Foo need a template argument, you can:

Foo<Foo<int>*> object; 
like image 161
songyuanyao Avatar answered Dec 07 '25 21:12

songyuanyao