Original Post
When I want to use strings in my C++ program why do I need to #include and also say... using std::string;
Quote:
Original post by frantic
#include
Quote:
using std::string;
std::string str;
#include <string>using namespace std;void main(){ string s = "astring";}#include <string>void main(){ std::string s = "astring";}namespace std{ // Stuff typedef basic_string<char, char_type<char>, allocator<char> > string; // More stuff}#include <string>int main(){ std::string test = "test"; return 0;}namespace foo { class example {}; class example_2 {};}namespace bar { class example {}; class example_2 {};}int main () { example a; /* example a: Do we mean foo::example or bar::example? This dosn't work to * prevent us from having this type of problem if, for example, we later * create another class of the same name in another namespace. */ foo::example b; /* example b: It's explicit and clear we want to use the example found in * the foo namespace. No matter how many new example classes we add into * other namespaces, this will remain clear. */ using bar::example; example c; /* example c: We explicitly state we're using bar::example, so from this * point onwards we'll know we're using bar::example and not foo::example * when we just say "example". */ using namespace bar; example_2 d; /* example d: We can also explicitly say we're using an entire namespace. * Same deal as with C, only it applies to all of the namespace's members. */ foo::example e; /* example e: However, we can allways explicitly say we want foo's example, * even after using statements for another namespace. */}Quote:
Original post by frantic
I've read up on namespaces and how they can be used to group classes/functions together etc so if the std namespace already includes the string class in it then why do I also need to '#include'
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more