user11498510
user11498510

Reputation:

Where to declare Enum

I need to use enum WrapMode in two differernt classes. what is the best approach.

1) Declare enum in a namespace in a header file and include in both the classes

2) locally declare the namespace in both the classes separately.

enum TextureWrapMode {  Repeat = 0, Clamp = 1};

class My_Texture
 {

 };

///////////////////////////////////////////////////////////////////////

class Texture
{

 };

Upvotes: 1

Views: 119

Answers (2)

Amit Sharma
Amit Sharma

Reputation: 319

If you are designing a big project or you have a lot of files where that particular enum is going to be used, so it better to put that enum or, I would say, all the common things like common structure in that .h file and include where you want.

And if you have a single file code that you should declare your enum right after the headers you have declared.

#include <iostream> 
using namespace std;

enum suit {
    club = 0,
    diamonds = 10,
    hearts = 20,
    spades = 3
} card;

int main() 
{
    card = club;
    cout << "Size of enum variable " << sizeof(card) << " bytes.";   
    return 0;
}

HOPE IT HELPS

Upvotes: 1

Demosthenes
Demosthenes

Reputation: 1535

Since you tagged your question c++11: Don't use enum, use enum class. Namespace issue (and more) solved! Unless I can't use C++11 for some reason, I don't see any reason for using enums anymore.

Other than that, I would put it into a header file if it is used by more than one module.

Upvotes: 1

Related Questions