Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a class inherit itself? [duplicate]

I don't understand the following code:

template <int _id> class Model;

template <int _id> class Model : public Model<0> { ... };

So, class Model derives from itself it seems. This doesn't compile with EDG or Gcc (error: invalid use of incomplete type ‘class Model<0>’), but Visual Studio accepts it. What compiler is right and for what reason?

like image 707
Paul Jansen Avatar asked Oct 27 '25 21:10

Paul Jansen


1 Answers

So, class Model derives from itself it seems.

The class doesn't inherit itself. Every instatiation of Model<N> is a different, unrelated class.

This doesn't compile with EDG or Gcc (error: invalid use of incomplete type ‘class Model<0>’), but Visual Studio accepts it. What compiler is right and for what reason?

GCC is correct, at the point of usage, Model<0> is incomplete. Inheritance requires a complete class declaration.

like image 162
user0042 Avatar answered Oct 29 '25 11:10

user0042