Sign in to follow this  
skwee

Does typedefs/defines must be inside namespaces?

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 this post


Link to post
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,

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