Global Constants in C++

Declaring the global constants can polute the names available and create conflicts as the project grow larger. One solution is to declare it using a namespace.

globalconst.h

namespace GlobalConst{

const double PI=3.14;

};

and in your file, simply use

GlobalConst::PI

UPDATE: I have corrected the code, thanks

Advertisements

One Response

  1. You forgot to mention the type of PI. Also, static is redundant because a const namespace scope object already implies internal linkage.

    const double PI = 3.1415926536;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: