• Advertisement

Archived

This topic is now archived and is closed to further replies.

[C++] Namespaces and types/enums

This topic is 5074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
damn tags

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
tried my proposal. didn''t work
And i bet the way you''re doing that stuff isn''t the most common one

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement