使用“struct Foo”在字段decl中不向前声明嵌套类 [英] use of "struct Foo" in field decl doesn't forward declare nested class

查看:191
本文介绍了使用“struct Foo”在字段decl中不向前声明嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显然误解C-ish特性如何同时声明和引用使用 struct X 在C ++中工作的类型。 (我甚至不知道这个功能被称为,正确。)我有三个例子,下面显示我在做什么,但主要的问题是:为什么是嵌套类型,以这种方式,在全局范围而不是在类范围内?

I am apparently misunderstanding how the C-ish feature of simultaneously "declaring" and referring to a type using struct X works in C++. (Plus I don't even know what this feature is called, properly.) I have three examples below to show what I'm doing but the main question is: Why is the nested type, forward declared in this way, at global scope instead of in class scope?

这个工作来转发声明嵌套结构:

This works to forward declare a nested struct:

#include <memory>
class Foo1
{
    Foo1();
    ~Foo1();
    struct Impl;
    std::auto_ptr<Impl> m_pimpl;
};

struct Foo1::Impl
{
    Impl(){}
    ~Impl(){}
};

Foo1::Foo1(){}
Foo1::~Foo1(){}

int main() {}

所以现在我想使用半记忆C特征保存几个字符,这样,我已经删除了 struct Impl; 行,并且声明其类型为 auto_ptr< struct Impl> 而不是 auto_ptr< Impl>

So now I want to save a few characters by using a half-remembered C feature, so see where I've dropped the line struct Impl; and for the field declared its type as auto_ptr<struct Impl> instead of auto_ptr<Impl>:

#include <memory>
class Foo2a
{
    Foo2a();
    ~Foo2a();
    std::auto_ptr<struct Impl> m_pimpl;
};

struct Foo2a::Impl
{
    Impl(){}
    ~Impl(){}
};

Foo2a::Foo2a(){}
Foo2a::~Foo2a(){}

int main() {}

这里编译器会在Foo2a中声明没有名为Impl的结构 。果然,它在全球范围内,因为编译:

Here the compiler complains that no struct named 'Impl' in 'Foo2a'. Sure enough, it is at global scope, as this compiles:

#include <memory>
class Foo2b
{
    Foo2b();
    ~Foo2b();
    std::auto_ptr<struct Impl> m_pimpl;
};

struct Impl
{
    Impl(){}
    ~Impl(){}
};

Foo2b::Foo2b(){}
Foo2b::~Foo2b(){}

int main() {}

这是什么? a)为什么它工作在所有声明类型 Impl 这种方式,和b)假定它的工作,为什么 Impl 在全局范围不是类范围?

What's up with that? a) Why does it work at all to "declare" the type Impl this way, and b) given that it works, why is Impl at global scope not class scope?

然后,BTW,声明该字段为 auto_ptr< struct Foo2c :: Impl& / code> 无法使用

And, BTW, declaring the field as auto_ptr<struct Foo2c::Impl> doesn't work either.

推荐答案

首先声明为 elaborated-type-specifier的名称的范围取决于它的使用方式。

The scope of a name first declared as elaborated-type-specifier depends on the way in which it is used.

如果你做 struct Impl; ,结构被声明为声明发生的任何范围的成员,如在你的第一个例子中。但在任何其他上下文中,它被声明在最近的封闭块或命名空间(而不是类),如第二和第三个示例中所示。

If you do struct Impl; the struct is declared as a member of whatever scope that declaration occurs in, as in your first example. But in any other context, it's declared in the nearest enclosing block or namespace (but not class), as in the second and third examples.

解决方案只是转发在任何其他使用之前,在类中将其解析为 struct Impl;

The solution is just to forward-declare it as struct Impl; in the class prior to any other uses. You can just refer to it as Impl after that.

请参阅[basic.scope.cdecl] / 7 for详细规则。

See [basic.scope.cdecl]/7 for the detailed rules.

这篇关于使用“struct Foo”在字段decl中不向前声明嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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