• 13
• 18
• 19
• 27
• 10

# Does typedefs/defines must be inside namespaces?

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

## Recommended Posts

Hello I want to do my own namespace but i have no idea if typedefs/defines and etc must be inside namesapce? What is the correct from beneath:
#include <iostream>
namespace myns{
typedef int myint;

class etc{
...
};
};

or
#include <iostream>

typedef int myint;

namespace myns{
class etc{
...
};
};

And one more question if i declare namespace in h/hpp file do i need to declare the same namespace in c/cpp file that is realization of h/hpp? Thanks a lot

##### Share on other sites
A define is just a preprocessing macro and has no bearing on the runtime structure of your program, at least not from the compiler's perspective (because the compiler never gets to see it). You may place it almost anywhere that you could otherwise place a preprocessing directive.

A typedef can go either outside a namespace (which makes it global and causes it to apply everywhere), or inside (which causes it to be scoped to that namespace). Typedefs are safer than defines because the compiler can check the type at compile time, whereas a define is simply a literal substitution ("replace all instances of this word with this one").

hope that helps,