[C++] Namespaces and types/enums

Started by
6 comments, last by jollyjeffers 20 years ago
Hello all. I know it aint the prettiest way of doing things, but I've got two big namespaces in our project - one for global variables; one for global types/enums/structs.. I'm basically wondering if the following is the right way of doing things... It all seems to compile fine, so I guess it isn't *wrong*, but is it *correct*!! For global variables nsVars.cpp
namespace MyNameSpaceVars {
    
    int iGlobal1;
    int iGlobal2;

}  
nsVars.h
namespace MyNameSpaceVars {

    extern int iGlobal1;
    extern int iGlobal2;

}  
Then, where any other module requires to read/write said globals, it #include's nsVars.h and then access it through MyNameSpaceVars::iGlobal1... For GLOBAL structs/types/enums Now this is the part that doesn't look so pretty, yet seems to work: nsTypes.h
namespace MyNameSpaceTypes {

    struct Vec3D {
        float x;
        float y;
        float z;
    }

    enum GAME_STATE {
        GS_INTRO = 1,
        GS_MENU = 2,
        GS_GAME = 3
    }

}  
and then anything that needs to declare/use the respective contents just #include's nsTypes.h and is declared:
MyNameSpaceTypes::Vec3D myVector;
myVector.x = 1; //and so on  
Now, its the enum's I don't like so much:
void SomeFunc( MyNameSpaceTypes::GAME_STATE gs ) {
    switch ( gs ) {
        case MyNameSpaceTypes::GS_INTRO:
            //do stuff when the intro is in progress
            break;

        case MyNameSpaceTypes::GS_MENU:
            //do stuff while user is in menu
            break;

        case MyNameSpaceTypes::GS_GAME:
            //do stuff when in game
            break;
    }
}  
This works / compiles. But accessing the values for the 'GAME_STATE' enum through the 'MyNameSpaceTypes' accessor seems less intuitive than using something like 'GAME_STATE::GS_MENU' or just 'GS_MENU' (I know I can get the latter by doing using namespace MyNameSpaceTypes; but I dont like doing it that way!) Sorry for the long ramble, but I'd appreciate peoples thoughts Best regards, Jack [edited by - Jollyjeffers on March 29, 2004 4:06:24 PM]

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Advertisement
quote:Original post by jollyjeffers
accessing the values for the ''GAME_STATE'' enum through the ''MyNameSpaceTypes'' accessor seems less intuitive than using something like ''GAME_STATE::GS_MENU'' or just ''GS_MENU'' (I know I can get the latter by doing <tt>using namespace MyNameSpaceTypes;</tt> but I dont like doing it that way!)

How about <tt>using MyNameSpaceTypes::GAME_STATE</tt>? Not sure if it works, though.
damn tags
quote:damn tags
lol

hmm, MyNameSpaceTypes::GAME_STATE::GS_GAME doesn''t work.

Having thought about it, I suppose its all equivelent to:

enum NORMAL_ENUM {   NE_SOMETHING = 1,   NE_SOMETHINGELSE = 2}void func( NORMAL_ENUM ne ) {  switch ( ne ) {    case NE_SOMETHING: ...    case NE_SOMETHINGELSE: ...  }} 


but with MyNameSpaceTypes:: prepended to everything...

I suppose what I was wondering is, the way I originally described - IS THAT HOW EVERYONE ELSE DOES IT??

cheers,
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

tried my proposal. didn''t work
And i bet the way you''re doing that stuff isn''t the most common one
quote:And i bet the way you''re doing that stuff isn''t the most common one


just what I thought, and hence why I made this post...

so, anyone got a better technique than the one I describe?

regards,
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

"replace switch with polymorphism"

see refactoring: improving the design of existing code

[edited by - petewood on March 30, 2004 10:39:20 AM]
Hello jollyjeffers,

just put:
using MyNameSpaceTypes;
at file scope or function scope.
then you can use
GS_INTRO instead of
MyNameSpaceTypes::GS_INTRO

void SomeFunc( MyNameSpaceTypes::GAME_STATE gs ) {    using MyNameSpaceTypes;    switch ( gs ) {        case GS_INTRO:            //do stuff when the intro is in progress            break;        case GS_MENU:            //do stuff while user is in menu            break;        case GS_GAME:            //do stuff when in game            break;    }}  


Ahh! to power of using.

Lord Bart

This topic is closed to new replies.

Advertisement