简体   繁体   English

在cpp和hpp文件中的整个程序使用的c ++中,在哪里声明枚举?

[英]Where to declare enum in c++ that is used by entire program, both in cpp and hpp files?

I have a program that performs simulations on networks. 我有一个程序可以在网络上执行仿真。 all my classes and implementations are in the h file, and I'm trying to call my classes' functions(whose are in the h file) from main.cpp. 我所有的类和实现都在h文件中,并且我试图从main.cpp调用类的函数(它们在h文件中)。 I use enumerations in my class, and the c'tor that's being called from main.cpp takes enum value. 我在类中使用枚举,并且从main.cpp调用的c'tor具有枚举值。 I keep getting errors because the cpp or h file doesn't know about these enumarations every time that I change the place of declaration. 我不断收到错误消息,因为每次更改声明位置时,cpp或h文件都不了解这些枚举。
1.should it be declared inside my class? 1.是否应该在我的课程中声明? outside? 外? both in cpp and h file? 都在cpp和h文件中? 2.if I also want that methods of the class will be able to use the enumerated types, where should I put my declarations? 2.如果我还希望该类的方法能够使用枚举类型,那么我应该在哪里声明? 3. What's the right way to get the enumerated type from both the cpp file and from methods of the class? 3.从cpp文件和类的方法中获取枚举类型的正确方法是什么? I saw a few options such as ClassName::enumType, CLassName.enumType and so on. 我看到了一些选项,例如ClassName :: enumType,CLassName.enumType等。 this is my code structure: 这是我的代码结构:

    main.cpp
    #include...

    enum networkType {X,Y};

    int main(){
    //create instances of my class 
    network n = new network(X, ...);


    }


    networks.h
    //enum networkType {X, Y};      here?
    class network{

        enum networkType {X, Y};         //here?
    network::network(networkType x, ...) //c'tor
    ..
//members:
    networkType type_;

//functions that use the enumerated value
    void setNetworkType(networkType t);
    networkType getNetworkType();
...more methods

some of the errors I'm getting are - for the getter method - 'networkType' does not name a type networkType network::getNetworkType() 我遇到的一些错误是-对于getter方法-'networkType'未命名类型networkType network :: getNetworkType()

Thanks. 谢谢。

If you place the enum definition inside the class, the actual name of the enum is network::networkType . 如果将enum定义放在类中,则枚举的实际名称为network::networkType You must use the full name any time you're outside the class scope (which includes the return type of a member function definition outside the class, unless you use trailing return types). 只要您不在类范围之内,就必须使用全名(除非使用尾随返回类型,否则该全名应包括类外部成员函数定义的返回类型)。

network::networkType network::getNetworkType()
{ return _type; }

(Note that if you also have enum network in main.cpp as you've shown, that's a different and incompatible type.) (请注意,如果如所示,如果在main.cpp中也有enum network ,则这是一个不同且不兼容的类型。)

1.should it be declared inside my class? 1.是否应该在我的课程中声明? outside? 外? both in cpp and h file? 都在cpp和h文件中?

enum networkType {X,Y}; should definitely not be at global scope, because this would pollute global scope with X and Y . 应该绝对不在全局范围内,因为这会污染XY全局范围。 No matter what, declaration of enum should always (and only) be in the header file of the class. 无论如何,枚举的声明应始终(且仅)位于类的头文件中。

Preferred C++11 declaration would be enum class networkType {X, Y} so X and Y will be scoped to networkType . 首选的C ++ 11声明将是enum class networkType {X, Y}因此XY范围将是networkType

If networkType is mostly used by class network , I would put its declaration within network as follows: 如果networkType主要由class network ,我将其声明放在network ,如下所示:

class network{
    enum class type {X, Y};
};

No need to add prefix "network" to the enum name, because type is now scoped to network . 无需在枚举名称中添加前缀“ network”,因为现在type的作用域为network

If you can't use C++11, I'd prefer this style: 如果您不能使用C ++ 11,我更喜欢这种风格:

class network{
    enum type {TYPE_X, TYPE_Y};
};

2.if I also want that methods of the class will be able to use the enumerated types, where should I put my declarations? 2.如果我还希望该类的方法能够使用枚举类型,那么我应该在哪里声明?

Declaration of methods and member variables can use nested types without prefix: 方法和成员变量的声明可以使用没有前缀的嵌套类型:

class network{
    enum class type {X, Y};
    network( type x, ... );
    type getNetworkType() const;  //< added const here, because method is only observer
    type _type;
}

When the implementation of a method is not inline, you have to add scope prefix: 如果方法的实现不是内联的,则必须添加作用域前缀:

network::network( network::type x, ... ){ 
    // within function body no scope prefix is needed:
    type y = x;
} 

network::type network::getNetworkType() const{ 
    return _type; 
}

Global functions have to use prefix in function header aswell as body: 全局函数必须在函数头和主体中使用前缀:

network::type do_something( network::type x ){
    network::type y = x;
    return y;
}
  1. What's the right way to get the enumerated type from both the cpp file and from methods of the class? 从cpp文件和类的方法中获取枚举类型的正确方法是什么?

"From methods of the class" - see above. “来自类的方法”-参见上文。

Within .cpp file you have to use scope prefix aswell. 在.cpp文件中,您还必须使用作用域前缀。 While we are at it, don't use operator new by default. 在执行此操作时,默认情况下不要使用new运算符。 In most cases, you can (and should) create your objects on the stack. 在大多数情况下,您可以(并且应该)在堆栈上创建对象。 If you really need dynamic allocation, prefer smart pointers like std::unique_ptr . 如果您确实需要动态分配,则喜欢使用诸如std::unique_ptr类的智能指针。 Modern C++ code should almost never contain a delete and rarely a new . 现代C ++代码几乎不应包含delete而几乎不应包含new

int main(){
    // Prefer to use stack variables:
    network net( network::type::X );
    network::type theType = net.getNetworkType();

    // If you really need dynamic allocation:
    auto pnet = std::make_unique<network>( network::type::X );
    theType = pnet->getNetworkType();
}

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

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