Sign in to follow this  
tmunq

Simple C++ question (namespaces & prototypes)

Recommended Posts

tmunq    144
So, how do I define a prototype for an object (struct, class, enum, etc..) that is hidden in a namespace? I've seriously tried pretty much everything.. Here is a simplified example: namespace MsgBox { enum Type {TOK,TYesNo}; enum Answer {AOK,AYes,ANo}; } // ... class IWinApp { public: MsgBox::Answer MsgBox(MsgBox::Type,char * Txt,...); // ... }; This works, but the namespace "MsgBox" has to be defined before anything using stuff in it. This is not a serious problem for my project but I'm just curious.. Anyway, Is there a way to define a prototype for a namespace? That should solve it.. [Edited by - tmunq on May 26, 2005 1:37:24 PM]

Share this post


Link to post
Share on other sites
Sharlin    864
Well, not really - the compiler needs to know what a "MsgBox::Type" before it can be used, independent of which namespace it resides in. A class/struct/union you could forward declare in a namespace, like this:
namespace foo { class bar; }

but enums cannot be forward declared -- and even if they could, you could only declare pointers and references to them, not use them, the same way as with classes.

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Anyway, I's there a way to define a prototype for a namespace?


A namespace's only purpose is to enclose stuff. There is no point in "prototyping" the namespace itself. You can open it and fill it with declarations if you wish, and then later reopen it and put the definitions.

namespace foo
{
class bar;
}

...

namespace foo
{
class bar
{
...
};
}

Share this post


Link to post
Share on other sites
tmunq    144
Thanks, Fruny. That is close enough to a prototype. :)

namespace MsgBox
{
enum Answer;
enum Type;
enum Picture;
}

// ...

class IWinApp
{
public:
// Displays a message box
virtual MsgBox::Answer MsgBox(MsgBox::Type Type,MsgBox::Picture Pic,char * Txt,...) = 0;
// ...
};

namespace MsgBox
{
enum Answer {AOK,AYes,ANo,ACancel,ARetry,AIgnore};
//...

This seems to work..

Share this post


Link to post
Share on other sites
SirLuthor    364
Quote:
Original post by Sharlin
Well, not really - the compiler needs to know what a "MsgBox::Type" before it can be used, independent of which namespace it resides in. A class/struct/union you could forward declare in a namespace, like this:
namespace foo { class bar; }

but enums cannot be forward declared -- and even if they could, you could only declare pointers and references to them, not use them, the same way as with classes.

Uhh, are you completely sure about that, that enums can not be forward declared? I do it all the time, haven't had any problems yet. Mainly, I use it so as to be able to use the enumerations as function arguments, etc.

Share this post


Link to post
Share on other sites
pragma Fury    343
An enum is a user-defined type, just like a class, so there's no reason you can't do this.. (in fact, you can overload operators on enums as well.. though that's not really relevant)

My question is, why would you wish to do this?

I suppose one reason would be to marginally improve compilation time, but that depends on your source-include structure.

I guess you could also use this to place the actual definition of the enum in a .cpp file, though I hardly think that's worth it.. you're just defining constants, and the compiled output will be identical either way.

My argument would be to define it in one place.. if I want to look up the enum's definition, I only have to look at one spot, instead of seeing the declaration and then trying to hunt down the definition.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this