5.2.4 · HinglishC++ Programming

Namespaces — avoiding name collisions

1,981 words9 min readRead in English

5.2.4 · Coding › C++ Programming


Namespace KYA hota hai?

namespace Geometry {
    double area(double r) { return 3.14159 * r * r; }   // a circle
}
namespace Statistics {
    double area(double base, double h) { return 0.5*base*h; } // unrelated "area"
}
 
int main() {
    double a = Geometry::area(2.0);        // 12.566...
    double b = Statistics::area(3.0, 4.0); // 6.0
}

Inki ZAROORAT kyon hai?


Figure — Namespaces — avoiding name collisions

Inhe USE kaise karein — teen access styles

#include <iostream>
 
void demo() {
    std::cout << "A\n";        // (1) qualified
 
    using std::cout;           // (2) sirf cout
    cout << "B\n";
 
    using namespace std;       // (3) sab kuch (headers mein avoid karo)
    cout << "C\n";
}

using namespace std; risky kyon hai

Yeh woh darwaza dobara khol deta hai jo humne band kiya tha: yeh saare std names ko current scope mein dump kar deta hai, toh tumhara distance, count, swap achanak std::distance wagera se clash kar sakta hai. Ek chote .cpp mein theek hai; header mein dangerous kyunki jo bhi file use include karti hai ushe yeh pollution mili jaati hai.


Lookup rule ko scratch se derive karna


Nested aur aliased namespaces

namespace Company {
    namespace Project {
        namespace Net { int port = 8080; }
    }
}
int x = Company::Project::Net::port;     // verbose!
 
namespace CPN = Company::Project::Net;   // namespace alias
int y = CPN::port;                       // tidy
 
// C++17 shorthand for nesting:
namespace Company::Project::Net { int retries = 3; }

Anonymous (unnamed) namespaces

namespace {            // unnamed → unique to THIS translation unit
    int secretHelper = 0;
    void onlyHere() {}
}

Common mistakes (steel-manned)


Worked examples


80/20 — vital few


Recall Feynman: ek 12-saal ke bacche ko samjhao

Class mein do bacche hain jinhe dono Sam kehte hain. Mix-up se bachne ke liye hum kehte hain "Sam Brown" aur "Sam Green" — surname se pata chalta hai kaun hai. Code mein, namespace surname hai. Jab tum Math::Sam likhte ho tumhara matlab Math-team wala Sam hai, doosra kabhi nahi. Agar tum sirf "Sam" kaho aur do Sams visible hain, teacher confuse ho jaata hai aur class rok leta hai (ek error) — isliye tum surname add karte ho clear hone ke liye.


Connections

  • Scope and Storage Duration — namespaces ek tarah ka named scope hain.
  • One Definition Rule (ODR) — collisions actually ODR violations hain.
  • Header Files and Include Guards — kyon using namespace headers mein spread hota hai.
  • Classes and Encapsulation — true privacy vs namespace grouping.
  • Linkage — internal vs external — unnamed namespaces aur static.
  • The Standard Library (std) — sabse bada real-world namespace.

Namespaces kya problem solve karte hain?
Name collisions — yeh identifiers ke full names ko unique banate hain taaki do libraries dono ek chhote naam jaise print use kar sakein.
Namespace member access karne wala operator kaunsa hai?
Scope-resolution operator ::, jaise Ns::member.
Using-declaration aur using-directive mein kya fark hai?
Using-declaration using std::cout; ek naam laata hai; using-directive using namespace std; saare naam laata hai.
Header mein using namespace std; kyun avoid karein?
Yeh har us file ko pollute karta hai jo header include karti hai, clashes ka risk aur silently altered name lookup ka khatre ke saath.
Kya namespaces privacy/hiding provide karte hain?
Nahi — members Ns::member se reachable rehte hain. Privacy ke liye classes ka private: ya unnamed namespaces use karo.
Unnamed namespace apne members ko kya deta hai?
Internal linkage — visibility us ek translation unit tak limited hoti hai (file-scope static ka modern replacement).
Leading ::name ka matlab kya hai?
Global namespace mein lookup start karo (locally using-inject kiye gaye names skip karo).
Kya namespaces files ke across open ya closed hain?
Open — tum same namespace mein multiple files mein dobara enter kar sakte ho; names accumulate hote hain (lekin har definition phir bhi ODR follow karta hai).
Company::Project::Net ko kaise shorten karein?
Namespace alias se: namespace CPN = Company::Project::Net;.
Do identifiers kab collide karte hain?
Tabhi jab unke fully-qualified names identical hoon jabki woh different entities ko refer karte hon.

Concept Map

cause

solves

declared with

makes unique

reached via

pattern

pattern

pattern

risks

dangerous in

is an

Name collisions

ODR violation or ambiguity

Namespace

namespace Name curly braces

Fully-qualified names

Scope-resolution operator ::

Qualified: std::cout

using-declaration: one name

using-directive: all names

Namespace pollution

Header files

Global scope