类如何继承自身? [英] How can a class inherit itself?
问题描述
我不理解以下代码:
template <int _id> class Model;
template <int _id> class Model : public Model<0> { ... };
因此,类Model似乎是从自身派生的.这不能与EDG或Gcc一起编译(错误:无效使用了不完整的类型'class Model< 0>'),但是Visual Studio接受了它.什么编译器是正确的,出于什么原因?
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?
推荐答案
因此,类Model似乎是从自身派生的.
So, class Model derives from itself it seems.
该类不会继承本身. Model<N>
的每个实例都是不同的,无关的类.
The class doesn't inherit itself. Every instatiation of Model<N>
is a different, unrelated class.
这不能与EDG或Gcc一起编译(错误:不完整类型'class Model< 0>'的无效使用),但是Visual Studio接受.什么编译器是正确的,出于什么原因?
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是正确的,在使用时,Model<0>
是不完整的.继承需要完整的类声明.
GCC is correct, at the point of usage, Model<0>
is incomplete. Inheritance requires a complete class declaration.
这篇关于类如何继承自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!