# Simple C++ question (namespaces & prototypes)

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

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
Thanks, Fruny. That is close enough to a prototype. :)

namespace MsgBox
{
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
{
//...

This seems to work..

##### Share on other sites
Quote:
 Original post by SharlinWell, 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 on other sites
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.

1. 1
Rutin
25
2. 2
3. 3
JoeJ
19
4. 4
5. 5

• 10
• 11
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631753
• Total Posts
3002098
×