C ++中枚举访问语义的基本原理 [英] Rationale behind enum access semantics in C++

查看:246
本文介绍了C ++中枚举访问语义的基本原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以对访问在C ++中定义的枚举的语义进行一些说明吗?



特别是,为什么枚举成员通过名称访问类而不是枚举本身?假设 enum 是容器/范围,就像命名空间 class ,当为枚举时,为什么访问容器的元素的处理不同于



给定

 命名空间mynamespace 
{
class myclass
{
public:
enum myenum
{
enum1,
enum2
};

int myint;
};
}

为什么 enum1 mynamespace :: myclass :: enum1 而不是 mynamespace :: myclass :: myenum :: enum1



当后者工作时,它不是推荐调用它的方式,一些编译器会在那里发出警告。 IMHO,它不应该是正确的,但它也应该是访问它的唯一方法。



这使得奇怪的访问规则,并使事情很奇怪,您在不同的枚举中添加一个新的 enum1 (此时必须添加限定符)。



它违背了枚举的目的。枚举的成员实际上是类的成员,而不是枚举,我必须说,我发现,在其他语言(例如C#)的行为是更可取的。



我想这是为了保持与C的兼容性,但我不明白为什么要求访问语义中的枚举名称将是更好的选择...我'

解决方案

在C中,选择 ++ 03,如在C中,枚举不引入新的作用域。定义的名称将进入周围范围。可能是命名空间或类。



C ++ 11添加了范围枚举和基于枚举。



普通C ++ 03 枚举

 枚举Cpp03 {a, b,c}; 

C ++ 11 based 枚举 ::

 枚举Cpp11Based:long {a,b,c} 

C ++ 11 scoped 枚举 (作为名称的作用域,如在您的示例中):

 枚举类Cpp11Scoped1 {a ,b,c}; 
enum struct Cpp11Scoped2 {a,b,c};

最后两种形式是等价的, Cpp11Scoped1 :: a



最后,范围枚举

 可以基于(指定名称的底层类型枚举类Cpp11ScopedAndBased:long {a,b,c}; 

一些C ++ 03编译器,包括Visual C ++,也为普通的C + +03 enum



我的猜测是你遇到的是语言扩展。

Can someone shed some light on the semantics for accessing an enum defined in a class in C++?

In particular, why are enum members accessed by the name of the class rather than the enum itself? Given that the enum is the container/scope, just as namespace and class are, why is accessing an element of the container treated differently when it's an enum than when it's a class?

Given

namespace mynamespace
{
    class myclass
    {
    public:
        enum myenum
        {
            enum1,
            enum2
        };

        int myint;
    };
}

Why is the fully-qualified name for enum1 mynamespace::myclass::enum1 and not mynamespace::myclass::myenum::enum1?

While the latter "works," it's not the "recommended" way of calling it and some compilers will throw a warning there. IMHO, it should not only be right, but it should also be the only way of accessing it.

It makes for really odd access rules, and makes things very weird when you add a new enum1 in a different enum (at which point you must add the qualifier).

Really, it defeats the purpose of an enum. The members of the enum are really more members of the class than they are of the enum, and I must say I find the behavior in other languages (e.g. C#) to be much more preferable.

I imagine this is to keep compatibility with C, but I don't see why requiring the enum name in the access semantic would be the better option... I'd imagine making the class name optional would be the option that preserves C compatibility.

解决方案

In C++03, as in C, an enum does not introduce a new scope. The names defined go into the surrounding scope. Which might be a namespace, or a class.

C++11 added scoped enums and based enums.

Ordinary C++03 enum:

enum Cpp03 { a, b, c };

C++11 based enum:

enum Cpp11Based: long { a, b, c };

C++11 scoped enum (which acts as a scope for the names, like in your example):

enum class Cpp11Scoped1 { a, b, c };
enum struct Cpp11Scoped2 { a, b, c };

The last two forms are equivalent, and allow writing e.g. Cpp11Scoped1::a.

Finally, a scoped enum can be based (specifying the underlying type for the names, i.e. size and signedness):

enum class Cpp11ScopedAndBased: long { a, b, c };

Some C++03 compilers, including Visual C++, provided the scope functionality also for the ordinary C++03 enum.

My guess is that what you have run into, a language extension.

这篇关于C ++中枚举访问语义的基本原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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