java嵌套类型 无法隐藏外层类型,嵌套类定义在外部类的外部,而外部类包含内部类的实例...

C++

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;

}

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

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 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.

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: define the two classes independently and link them with a nested typedef.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值