如何在不将内部类的定义置于父类的情况下创建内部类? [英] How to make an inner class without putting the definition of inner class to parent class?
问题描述
我要写一个头文件,它很长.由于它太复杂了,所以我不想将内部类的定义放到根类中.我的意思是我如何在不编写内部类的情况下将其内部化根类.
I'll write an header file,and it's very long.Since it will be too complicated,i don't want to put inner class definition in root class.I mean how can i make a class inner without writing it in root class.
class outer
{
}
class inner
{
}
如果我可以那样使用,我认为头文件会更清楚.
If i can use like that, The header file will be clearer i think.
推荐答案
像这样:
// foo.hpp
class Foo
{
public:
class Inner;
Foo();
void bar();
Inner zoo();
};
// foo_inner.hpp
#include "foo.hpp"
class Foo::Inner
{
void func();
};
然后,在实现中:
#include "foo.hpp"
#include "foo_inner.hpp"
void Foo::bar() { /* ... */ }
void Foo::Inner::func() { /* ... */ }
请注意,您可以在Foo
的类定义(即foo.hpp
中)内使用不完整类型Foo::Inner
,但要遵守对不完整类型的通常限制,例如Inner
可能显示为函数返回类型,函数参数,引用或指针.只要类Foo
的成员函数实现可以看到Foo::Inner
的类定义(通过包含foo_inner.hpp
),一切都很好.
Note that you can use the incomplete type Foo::Inner
inside the class definition of Foo
(i.e. in foo.hpp
) subject to the usual restrictions for incomplete types, e.g. Inner
may appear as a function return type, function argument, reference, or pointer. As long as the member function implementations for the class Foo
can see the class definition of Foo::Inner
(by including foo_inner.hpp
), all is well.
这篇关于如何在不将内部类的定义置于父类的情况下创建内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!