简体   繁体   English

C ++从另一个C ++文件“导入”枚举类

[英]C++ “Importing” an enum class from another C++ file

I'm a coming from a Java background so please bear with me on this :) 我来自Java背景,所以请耐心等待:)

I'm trying to "import" (to use Java terminology) an enum class from another C++ file so as to be able to use it directly without having to prefix it with the class name. 我正在尝试从另一个C ++文件“导入”(使用Java术语) enum class ,以便能够直接使用它,而不必使用类名称作为前缀。

So for instance, let's say I have this in a header file: 例如,假设我在头文件中有这个:

class Foo
{
    public:
        enum class Bar {ITEM_1, ITEM_2};
        void doThings(Bar bar);
};

Now, currently, if I want to use Bar from another C++ file, I do this: 现在,目前,如果我想从另一个C ++文件使用Bar ,我这样做:

#include "Foo.h"

void Foo2::methodInAnotherFile()
{
    Foo foo();
    Foo::Bar bar = Foo::Bar::ITEM_2;
    foo.doThings(bar);
}

Now what I'd like to do is to be able to do something like "import" (to use Java terminology) Bar so as to be able to remove the need to prefix Bar with Foo:: , ie do Bar bar = Bar::ITEM_2; 现在我想做的是能够做一些像“import”(使用Java术语) Bar这样的东西,以便能够删除使用Foo::前缀Bar的需要,即做Bar bar = Bar::ITEM_2; .

Now, with my limited knowledge of C++, one way I can think of doing this would be to surround all the code in Foo.h with namespace FooNamespace{} , take the Bar enum out of the class (but still in the namespace), and then add using namespace FooNamespace to the top of the Foo2 class. 现在,由于我对C ++的了解有限,我可以想到这样做的一种方法是用namespace FooNamespace{}包围Foo.h所有代码,将Bar enum从类中取出(但仍然在命名空间中),然后using namespace FooNamespace添加到Foo2类的顶部。 However, for my application that really doesn't make much logical sense, as the Bar enum really logically belongs inside that Foo class. 但是,对于我的应用程序来说真的没有太多的逻辑意义,因为Bar enum在逻辑上真正属于Foo类。

Since I'm proficient in Java, here's a Java example of what I'd like to do: 由于我精通Java,这里是我想做的Java示例:

File 1: 档案1:

package org.fooclass;

public class Foo
{
    public static enum Bar
    {
        ITEM_1,
        ITEM_2;
    }

    public void doThings(Bar bar)
    {
        System.out.println("Item: " + bar.toString());
    }
}

File 2: 文件2:

package org.foo2class;

import org.fooclass.Foo;
import org.fooclass.Foo.Bar; //I want to do THIS in C++

public class Foo2
{
    public void methodInAnotherFile()
    {
        Foo foo = new Foo();

        /*
         * Since I've 'imported' Foo.Bar, I can now
         * use Bar directly instead of having to do this:
         * Foo.Bar bar = Foo.Bar.ITEM2;
         */
        Bar bar = Bar.ITEM_2;

        foo.doThings(bar);
    }
}

Use a type alias via a using statement, eg: 通过using语句使用类型别名 ,例如:

#include "Foo.h"

using Bar = Foo::Bar; // <-- here

void Foo2::methodInAnotherFile()
{
    Foo foo;
    Bar bar = Bar::ITEM_2;
    foo.doThings(bar);
}

Or, to limit its scope: 或者,限制其范围:

#include "Foo.h"

void Foo2::methodInAnotherFile()
{
    using Bar = Foo::Bar; // <-- here
    Foo foo;
    Bar bar = Bar::ITEM_2;
    foo.doThings(bar);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM