Question

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

 45  69867  45
1 Jan 1970

Solution

 65

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.

2009-08-16

Solution

 19

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.

2009-08-16