Simple C++ question (namespaces & prototypes)

Started by
4 comments, last by pragma Fury 18 years, 11 months ago
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]
Advertisement
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.
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   {     ...   };}
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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..
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.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
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.

This topic is closed to new replies.

Advertisement