user2532296
user2532296

Reputation: 848

why to use classes inside namespace

I wish to know the actual reason behind writing the class names inside namespace. What purpose do they solve? Do they include the classes or do something else? Any help is greatly appreciated.

namespace ns3 { 
class Channel;
class SpectrumChannel;
class MyMod;                        ;

class NewModule : public NetDevice
{   
public:
// methods and data
}

Upvotes: 0

Views: 183

Answers (1)

coproc
coproc

Reputation: 6257

It is good practice to put your classes into a namespace in order to avoid a name collision with other code (libraries) you might use (also coming in later in a project). When using common names like Vector, Logger, etc. as class names it can easily happen that they are also used in other code you want to use. When you put your classes into a (well considered) namespace, this chance of a name-collision is minimized.

Forward declarations are a different (independent) topic. When classes are defined within a namespace, also their forward declarations must be done within that namespace.

Upvotes: 2

Related Questions