嵌套类定义在外部类的外部,而外部类包含内部类的实例 [英] Nested class definition outside outer class's, while outer class contains instance of inner class

查看:171
本文介绍了嵌套类定义在外部类的外部,而外部类包含内部类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++

如何将内部(嵌套)类的定义放在外部(封闭)类的定义之外,其中外部类至少具有一个内部类的实例作为数据成员?我搜索了但找到的最相关的SO答案,源中的嵌套类定义文件,没有外部类将内部对象作为数据成员的示例.我遵循了这个答案,至于在外部类的定义中声明但未定义内部类,但是我的代码仍然坏了:

How can I put the definition of an inner (nested) class outside its outer (enclosing) class's definition, where the outer class has at least one instance of the inner class as a data member? I searched but the most relevant SO answer I found, Nested Class Definition in source file, does not have an example where the outer class has an inner object as a data member. I followed that answer, as far as declaring but not defining the inner class inside the outer class's definition is concerned, but my code is still broken:

struct Outer
{
    struct Inner;
    Inner myinner;
    Outer() : myinner(2) {}
};

struct Outer::Inner
{
    Inner(int n) : num(n) {}
    int num;
};

int main()
{
    Outer myouter;
}

它在VC11中给出错误error C2079: 'Outer::myinner' uses undefined struct 'Outer::Inner'.

It gives the error error C2079: 'Outer::myinner' uses undefined struct 'Outer::Inner' in VC11.

为什么像下面的工作代码那样,损坏的代码的效果不等同于在Outer的定义内定义Inner的版本的效果?

And why doesn't the broken code have an effect equivalent to that of the version in which Inner is defined within Outer's definition, like in the following working code?

struct Outer
{
    struct Inner
    {
        Inner(int n) : num(n) {}
        int num;
    } myinner;
    Outer() : myinner(2) {}
};

推荐答案

这是一个危险信号,但是您可以使用伪造的模板来做到这一点.

This is a red flag, but you can do it using a fake template.

template< typename = void >
struct Outer_temp
{
    struct Inner;
    Inner myinner;
    Outer_temp() : myinner(2) {}
};

typedef Outer_temp<> Outer; // Hide template from user.

template< typename v >
struct Outer_temp< v >::Inner
{
    Inner(int n) : num(n) {}
    int num;
};

int main()
{
    Outer myouter;
}

模板内部的

Inner是从属类型,因此在成员或任何其他上下文中定义实例时,它不需要是完整的.仅在实例化发生后才需要完成(在本例中为main.

Inner inside the template is a dependent type, so it does not need to be complete as you define an instance, in a member or any other context. It only needs to be complete once the instantiation happens, in this case from main.

我无法想象这样做的充分理由,但是确实如此.

I can't imagine a good reason to do this, but there it is.

嵌套类不应用于程序组织.嵌套暗示了一个概念上的依赖性,内部不能存在,除非在外部提供的上下文中".例如,虽然通常将容器节点类嵌套在容器中,但这可能会引起问题. SCARY惯用语是一种拒绝此类组织并获得改进的通用性的设计风格.

Nested classes should not be used for the sake of program organization. Nesting suggests a conceptual dependency, "Inner cannot exist except in a context provided by Outer." Although it is common, for example, for a container node class to be nested within the container, this can cause problems. The SCARY idiom is a design style that repudiates such organization and gains improved genericity.

TL; DR:分别定义两个类,并将它们与嵌套的typedef链接.

TL;DR: define the two classes independently and link them with a nested typedef.

这篇关于嵌套类定义在外部类的外部,而外部类包含内部类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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