嵌套的C ++类能否继承它的封闭类? [英] Can a nested C++ class inherit its enclosing class?

查看:92
本文介绍了嵌套的C ++类能否继承它的封闭类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试以下操作:

class Animal
{
    class Bear : public Animal
    {
        // …
    };

    class Giraffe : public Animal
    {
        // …
    };
};

...但是我的编译器看起来很尴尬。这是合法的C ++,如果没有,是否有更好的方法来完成同样的事情?基本上,我想创建一个更清洁的类命名方案。 (我不想从一个公共基类派生 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)

推荐答案

你可以做你想要的,但是你必须延迟嵌套类的定义。

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
{
};

这篇关于嵌套的C ++类能否继承它的封闭类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆