tags:

views:

315

answers:

7

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

+2  A: 

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

mipadi
@mipadi - Where is the proper place for it? Are they considered public, protected, private? Does it go with the class definitions?
If you put it outside of a class definition, I think it's just globally available. (Coming from a C background here, I don't write much C++ code.)
mipadi
@mipadi - got it. I will try it and see what happens. It is private to the class, maybe putting it under private will work.
If it's in a header file and not in a namespace block, it will be available in the global namespace to every compilation unit that includes the header file. If you want to limit accessibility, can put an enum inside of a class and make it protected or private.
Tyler McHenry
@tyler - it is in a namespace block. So the code as written above, if scope is not specified before 'enum' then it is protected or private by default?
YOu'd put it in a header file if you need to share the enum definition between several .cpp files. Put it in the cpp file where it's used if only that one .cpp file needs it-
nos
@indiecompanyll Namespaces have no concept of public and private. Only classes (and structs) do. Placing it in a namespace block will prevent it from polluting the global namespace, but only placing it in the private part of a class definition will make it class-private, which seems to be what you want.
Tyler McHenry
+2  A: 

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.

Andrew Medico
function definition (without include)
Martin York
A: 

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.

Johannes Schaub - litb
A: 

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.

Matthieu
+1  A: 

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.

Also, Never Ever have:

using namespace <name>;

in a header as this can cause strange ambiguity problems.

quamrana
@quamrana - all of these headers have using namespace <name>! I am not the original author, but both headers and source have it in all files..if the headers are just for this project is that OK. What is the best action to take?
@indiecompanyll - using namespace <name> should ONLY be used in module (.cpp) files. With statements like these in header files, it brings names into the global namespace which may conflict with other libraries you happen to use, or your own names. At best there may not be any problems. You may have to reference your own names like this: namespace::name. At worst you may have to wrap the classes and functions from the offending library inside new classes and functions that just forward on.
quamrana
+6  A: 

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.

IAmFledge
A: 

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.

Zeroshade