Eitanchuk
Eitanchuk

Reputation: 11

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. I use enumerations in my class, and the c'tor that's being called from main.cpp takes enum value. 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.
1.should it be declared inside my class? outside? both in cpp and h file? 2.if I also want that methods of the class will be able to use the enumerated types, where should I put my declarations? 3. What's the right way to get the enumerated type from both the cpp file and from methods of the class? I saw a few options such as ClassName::enumType, CLassName.enumType and so on. 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()

Thanks.

Upvotes: 1

Views: 1748

Answers (2)

zett42
zett42

Reputation: 27756

1.should it be declared inside my class? outside? both in cpp and h file?

enum networkType {X,Y}; should definitely not be at global scope, because this would pollute global scope with X and Y. 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.

If networkType is mostly used by class network, I would put its declaration within network as follows:

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

No need to add prefix "network" to the enum name, because type is now scoped to network.

If you can't use C++11, I'd prefer this style:

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?

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?

"From methods of the class" - see above.

Within .cpp file you have to use scope prefix aswell. While we are at it, don't use operator new by default. 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. Modern C++ code should almost never contain a delete and rarely a 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();
}

Upvotes: 0

aschepler
aschepler

Reputation: 72271

If you place the enum definition inside the class, the actual name of the enum is 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.)

Upvotes: 1

Related Questions