user147502
user147502

Reputation: 1118

Enums: Can they do in .h or must stay in .cpp?

If I have something like:

enum
{
    kCP_AboutBox_IconViewID = 1,
    kCP_AboutBox_AppNameViewID = 2,
    kCP_AboutBox_VersionViewID = 3,
    kCP_AboutBox_DescriptionViewID = 4,
    kCP_AboutBox_CopyrightViewID = 5
};

in my .cpp can it go in the .h?

More so, what other lesser know things can you put in a .h besides class definitions, variables, etc, etc

Upvotes: 45

Views: 73144

Answers (7)

quamrana
quamrana

Reputation: 39354

Remember to use header include guards in headers like:

#ifndef header_name_h
#define header_name_h
...
#endif

This helps you to keep to the one definition rule when multiple headers include your header.

Update:

I have since found that the latest versions of Visual Studio and gcc both allow:

#pragma once

Also, Never Ever have:

using namespace <name>;

in a header as this can cause strange ambiguity problems.

Upvotes: 19

Zeroshade
Zeroshade

Reputation: 573

In general an enum is going to be used as a type definition and should always be in the header file. Things to think about are the scope of it.

If the enum is just placed outside any scope in the header, it will be globally available to any thing that includes the header file. If you instead want the enum only accessible to the class itself, you can place it in the private section of the class.

In general you shouldn't make the enum globally scoped, instead you should either put it in a namespace or in the public section of a class. Then you can access the enum by way of

NamespaceOrClass::EnumValue

Also, as a sidenote, enums automatically iterate values from the first one you give (or 0).

enum
{
    kCP_AboutBox_IconViewID = 1,
    kCP_AboutBox_AppNameViewID = 2,
    kCP_AboutBox_VersionViewID = 3,
    kCP_AboutBox_DescriptionViewID = 4,
    kCP_AboutBox_CopyrightViewID = 5
};

Is exactly the same as

enum
{
    kCP_AboutBox_IconViewID = 1,
    kCP_AboutBox_AppNameViewID,
    kCP_AboutBox_VersionViewID,
    kCP_AboutBox_DescriptionViewID,
    kCP_AboutBox_CopyrightViewID
};

It's not a problem or error, just stylistic really.

Upvotes: 4

RemoteCTO
RemoteCTO

Reputation: 828

A .h file is essentially just code which, at compile time, is placed above any .cpp (or .h file for that matter) that it's included in. Therefore you CAN just place any code from the .cpp file into the .h and it should compile fine.

However it's the design which is important. Your code (e.g. your enum) SHOULD be placed in the .h file if you need to expose it to the code you're including the .h file. However if the enum is only specific to the code in your header's .cpp implementation, then you should encapsulate it just within the .cpp file.

Upvotes: 66

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506995

The one definition rule allows that at 3.2/5. All of the following can be put into headers, and be included multiple times into different translation units.

There can be more than one definition of a class type (clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (clause 14), non-static function template (14.5.5), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.4) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements.

The requirements essentially come down to that each definition must be the same. Note that if your enumeration type itself has no name, then it's not covered by that rule. Each definition of it in another translation unit defines a new enumeration type then, and doesn't clash with each other.

Putting them into the header is a good place if it's supposed to be public. Putting them in the implementation file is a good place if it's supposed to be private to that single file. In the latter case, either put them into an unnamed namespace, or make them unnamed (like it's the case with your enumeration example), so that it can't clash with another enumeration having the same name.

Upvotes: 3

Matthieu
Matthieu

Reputation: 2761

I have not enough information, but maybe you can declare the enum not only in the .h but inside the class. Remember to keep the scope of a variable to a minimum.
if the enum is relevant to a particular class you should declare it inside the class.

Upvotes: 0

nobody
nobody

Reputation: 20174

Yes, of course you can put it in a .h file. The only things that shouldn't go in a .h file are things that would cause a problem if they get included into more than one object, such as global object initializers.

Upvotes: 4

mipadi
mipadi

Reputation: 410662

Yes, your enum definition can go in your header (.h) file. Don't repeat the definition in your .cpp file, though.

Upvotes: 10

Related Questions