Created
April 13, 2014 20:21
-
-
Save axrwkr/10600719 to your computer and use it in GitHub Desktop.
C++ Scope Operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
on os x compile with | |
clang++ scope_operator.cpp -o scope_operator | |
on windows compile with | |
cl /EHsc scope_operator.cpp | |
on linux compile with | |
g++ scope_operator.cpp -o scope | |
*/ | |
#include <iostream> | |
void Buy(std::string thing) | |
{ | |
std::cout << "Buying " << thing << std::endl; | |
} | |
void Read(std::string thing) | |
{ | |
std::cout << "Reading " << thing << std::endl; | |
} | |
void Read() | |
{ | |
Read("something"); | |
} | |
namespace Advert | |
{ | |
void Read(std::string thing) | |
{ | |
std::cout << "Reading an Advert in "; | |
std::cout << thing << std::endl; | |
} | |
} | |
namespace Book | |
{ | |
void Read() | |
{ | |
// call the Read function in the global namespace | |
::Read("a Book"); | |
} | |
} | |
namespace Magazine | |
{ | |
void Read() | |
{ | |
// call the Read function in the global namespace | |
::Read("a Magazine"); | |
// call the Read function in the Advert namespace | |
Advert::Read("a Magazine"); | |
// call the Buy function in the global namespace | |
Buy("a Magazine"); | |
} | |
} | |
namespace Newspaper | |
{ | |
void Read(std::string thing) | |
{ | |
std::cout << "Reading " << thing; | |
std::cout << " in the Newspaper"; | |
std::cout << std::endl; | |
} | |
void Read() | |
{ | |
// call the Read function in the global namespace | |
::Read("a Newspaper"); | |
// call the Read function in the Newspaper namespace | |
Read("an article"); | |
// call the Read function in the Advert namespace | |
Advert::Read("a Newspaper"); | |
} | |
} | |
int main() | |
{ | |
// call the Read function from the global namespace | |
Read(); | |
// call the Read function from the Book namespace | |
Book::Read(); | |
// call the Read function from the Magazine namespace | |
Magazine::Read(); | |
// call the Read function from the Newspaper namespace | |
Newspaper::Read(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment