Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

What's The Difference.....

....between "std::string whatever;" and "string whatever;"?

Thanks,
Tony
0
fattumsdad
Asked:
fattumsdad
  • 2
2 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
a little difference:

both are refered to the same class from STL library, but first version specifies "namespace". Namespaces allows you to avoid homonime classes by separating in different "namespaces", but if you want to access a particular namespace frecuently you can use this sentence:

using namespace namespace_here;

So, to simplify the first version you must use:

using namespace std;   // put this at the beginning of your file
string whatever;

Alternatively:

std::string whatever;

If you write:

string whatever;

you will have an "unrecognized class name" because it is not in the "global" namespace.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Forgot to mention that "std" is the namespace name for STL library.
0
 
AxterCommented:
>>....between "std::string whatever;" and "string whatever;"?

Just to add to above comments, it's common practice to add using namespace std to your *.cpp file (implementation).

However, you should never put this in your header file.
In your header file you should always reference objects by full qualified name.

class foo
{
   std::string data;
};

The reason why you don't want to add using namespace std to your header is because any code that uses your header will get the namespace, and this can conflict with other code or other namespace.

So, it's ok to use using namespace std in your *.cpp file, but not OK for the header.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now