I’m trying to do the following:
我正在尝试执行以下操作:
class Animal
{
class Bear : public Animal
{
// …
};
class Giraffe : public Animal
{
// …
};
};
… but my compiler appears to choke on this. Is this legal C++, and if not, is there a better way to accomplish the same thing? Essentially, I want to create a cleaner class naming scheme. (I don’t want to derive Animal
and the inner classes from a common base class)
...但是我的编译器似乎对此嗤之以鼻。这是合法的C ++,如果没有,是否有更好的方法来完成同样的事情?基本上,我想创建一个更清晰的类命名方案。 (我不想从一个公共基类派生Animal和内部类)
5 个解决方案
#1
40
You can do what you want, but you have to delay the definition of the nested classes.
您可以执行所需的操作,但必须延迟嵌套类的定义。
class Animal
{
class Bear;
class Giraffe;
};
class Animal::Bear : public Animal {};
class Animal::Giraffe : public Animal {};
#2
15
The class type is considered incomplete until the end of its definition is reached. You cannot inherit from incomplete class so you cannot inherit from enclosing class.
在到达定义的末尾之前,类类型被认为是不完整的。您不能从不完整的类继承,因此您不能从封闭的类继承。
EDIT: Correction
As Richard Wolf corrected me: It is possible to inherit from enclosing class if you delay the definition of the nested classes. See his answer for details.
编辑:更正正如理查德沃尔夫纠正我:如果你延迟嵌套类的定义,可以继承封闭类。详情请见他的回答。
#3
2
Doesn't really answer the question, but I think you're misusing nested class, maybe you should have a look at namespaces (by the way, the answer is "it's not possible, because Animal is an incomplete type").
没有真正回答这个问题,但我认为你是在滥用嵌套类,也许你应该看一下命名空间(顺便说一句,答案是“它不可能,因为Animal是一个不完整的类型”)。
#4
2
I'm not clear what you are trying to achieve here but you may be able to get it via namespaces.
我不清楚你想在这里实现什么,但你可以通过命名空间获得它。
namespace creatures
{
class Animal
{
};
class Bear : public Animal
{
};
class Giraffe : public Animal
{
};
}
This declares Bear and Giraffe as types of animals but puts the whole thing in a namespace if you are looking for the base class to not pollute the global namespace.
这将Bear和Giraffe声明为动物的类型,但如果您正在寻找不污染全局命名空间的基类,则将整个事物放在命名空间中。
#5
0
It's always worth considering the ATL method of "Upside-Down Inheritance". It makes my head hurt every time I need to do this, but the efficiency of the object-code you get is unbeatable. Somewhere I clipped the article by Jim Beveridge which was the best explanation I ever saw, but this is hard to find today, I just see a reference to it.
总是值得考虑“倒置继承”的ATL方法。每次我需要这样做时都会让我的头受伤,但是你获得的对象代码的效率是无与伦比的。在某个地方,我剪切了Jim Beveridge的文章,这是我见过的最好的解释,但今天很难找到,我只是看到它的参考。
"Mar 25, 2004 ... An excellent article from Jim Beveridge: ATL and Upside-Down Inheritance.."
“2004年3月25日......来自Jim Beveridge的优秀文章:ATL和颠倒传承......”
#1
40
You can do what you want, but you have to delay the definition of the nested classes.
您可以执行所需的操作,但必须延迟嵌套类的定义。
class Animal
{
class Bear;
class Giraffe;
};
class Animal::Bear : public Animal {};
class Animal::Giraffe : public Animal {};
#2
15
The class type is considered incomplete until the end of its definition is reached. You cannot inherit from incomplete class so you cannot inherit from enclosing class.
在到达定义的末尾之前,类类型被认为是不完整的。您不能从不完整的类继承,因此您不能从封闭的类继承。
EDIT: Correction
As Richard Wolf corrected me: It is possible to inherit from enclosing class if you delay the definition of the nested classes. See his answer for details.
编辑:更正正如理查德沃尔夫纠正我:如果你延迟嵌套类的定义,可以继承封闭类。详情请见他的回答。
#3
2
Doesn't really answer the question, but I think you're misusing nested class, maybe you should have a look at namespaces (by the way, the answer is "it's not possible, because Animal is an incomplete type").
没有真正回答这个问题,但我认为你是在滥用嵌套类,也许你应该看一下命名空间(顺便说一句,答案是“它不可能,因为Animal是一个不完整的类型”)。
#4
2
I'm not clear what you are trying to achieve here but you may be able to get it via namespaces.
我不清楚你想在这里实现什么,但你可以通过命名空间获得它。
namespace creatures
{
class Animal
{
};
class Bear : public Animal
{
};
class Giraffe : public Animal
{
};
}
This declares Bear and Giraffe as types of animals but puts the whole thing in a namespace if you are looking for the base class to not pollute the global namespace.
这将Bear和Giraffe声明为动物的类型,但如果您正在寻找不污染全局命名空间的基类,则将整个事物放在命名空间中。
#5
0
It's always worth considering the ATL method of "Upside-Down Inheritance". It makes my head hurt every time I need to do this, but the efficiency of the object-code you get is unbeatable. Somewhere I clipped the article by Jim Beveridge which was the best explanation I ever saw, but this is hard to find today, I just see a reference to it.
总是值得考虑“倒置继承”的ATL方法。每次我需要这样做时都会让我的头受伤,但是你获得的对象代码的效率是无与伦比的。在某个地方,我剪切了Jim Beveridge的文章,这是我见过的最好的解释,但今天很难找到,我只是看到它的参考。
"Mar 25, 2004 ... An excellent article from Jim Beveridge: ATL and Upside-Down Inheritance.."
“2004年3月25日......来自Jim Beveridge的优秀文章:ATL和颠倒传承......”