Solved

What's The Difference.....

Posted on 2004-09-07
3
223 Views
Last Modified: 2010-04-01
....between "std::string whatever;" and "string whatever;"?

Thanks,
Tony
0
Comment
Question by:fattumsdad
  • 2
3 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
ID: 12001814
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12001832
Forgot to mention that "std" is the namespace name for STL library.
0
 
LVL 30

Accepted Solution

by:
Axter earned 25 total points
ID: 12002350
>>....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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question