正确的方法使用枚举值传递值和函数中的返回? [英] Correct way to use enums with pass-by-value and return in functions?

查看:230
本文介绍了正确的方法使用枚举值传递值和函数中的返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道问题标题是非常模糊的,因此正文:)

I understand the question title is mightily vague, hence the body text :)

我有几个枚举我用于识别文件类型和其他需要轻松区分的东西。我之前的做法是这样的:

I have several enums I use for identifying file types and other stuff that needs easy differentiating. My former approach was this:

namespace my_namespace
{
    namespace fileType
    {
        enum fileType
        {
            SOURCE,
            HEADER,
            RESOURCE
        };
    }
}
using namespace my_namespace::fileType;

这允许我定义一个函数:

Which allowed me to define a function:

fileType someFunction( const std::string &someFile, const fileType someType )
{
    //...
    return fileType::HEADER;
}

我可以定义和比较这样的变量:

And I could define and compare variables like this:

fileType type = fileType::SOURCE;

这是真棒。虽然有一些警告。标题(没有任何使用指令)需要使目标枚举名称加倍,以使编译器知道您正在使用类型,而不是命名空间:

Which is awesome. Though there were some caveats. Headers (without any using directives) required doubling the intended enum name to let the compiler know you're using the type, not namespace:

my_namespace::fileType::fileType soeFunction( const std::string &someFile, const my_namespace::fileType::fileType someType );

这看起来很傻,很难读懂,此外,MSVC抱怨在使用非标准扩展的警告级别1(由于例子中的doule fileType )。奇怪的是,GCC不会在最严格的环境下抱怨,但嘿,这是一个不同的故事。

Which does look silly, is hard to read and painful to understand. Additionally, MSVC complains at warning level one about a non-standard extension used (due to the doule fileType in the example). Strange that GCC does not complain at the strictest settings, but hey, that's a different story.

现在我要重写我的枚举 s以某种方式(匿名地)包含在struct而不是命名空间中,从而允许在声明函数时进行单个限定,从而关闭MSVC的警告。但是在这种情况下,我如何写 return 语句。是否绝对需要提供一个构造函数/转换操作符,还是有一个我没有看到的方法。

I now want to rewrite my enums in a way that they are (anonymously) enclosed in a struct instead of a namespace, allowing for the single qualification when declaring functions, thus shutting up MSVC's warning. But How do I write the return statement in this case. Is it absolutely necessary to provide a constructor/conversion operator or is there a way around this I did not see?

示例:

// enum definition
namespace my_namespace
{
    struct fileType
    {
        enum
        {
            SOURCE,
            HEADER,
            RESOURCE
        };
    }
}
using my_namespace::fileType;

// function declaration in header
my_namespace::fileType someFunction( const std::string &s, const my_namespace::fileType type );

// function implementation in .cpp file
using my_namespace::fileType;

fileType someFunction( const string &s, const fileType type )
{
    //...(problem is situated below)
    return fileType::SOURCE;
}

这说明了我想做什么。我想避免显式调用枚举结构的构造函数: fileType(fileType :: SOURCE)这将留下一个双 fileType 使用。

This illustrated what I'd like to do. I'd like to avoid explicitely calling the enum struct's constructor: fileType(fileType::SOURCE) which would leave me with a double fileType use.

感谢您的帮助!

PS:如果此问题已回答,我道歉,但我没有找到一个很好的选择与谷歌或SO的以前的问题这个问题。

PS: if this question has been answered before, I apologize, but I didn't find a good alternative with google or on SO's previous questions on this subject.

推荐答案

我使用一个非常简单的技巧:

Personally I use a very simple trick:

struct EnumName
{
  enum type {
    MemberOne,
    MemberTwo,
    ...
  };
};

typedef EnumName::type EnumName_t;

// Usage
EnumName_t foo = EnumName::MemberOne;

在C ++ 0x中,您可以直接使用范围内的枚举器:

In C++0x, you can have scoped enumerators directly:

enum struct EnunName // note: struct and class equivalent here
{
  MemberOne,
  MemberTwo,
  ...
};

// Usage
EnumName foo = EnumName::MemberOne;

这真的很棒:)

注意:scoped枚举也不受整体促销,这是非常好的

这篇关于正确的方法使用枚举值传递值和函数中的返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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