Solved

What's The Difference.....

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

Thanks,
Tony
0
Comment
Question by:fattumsdad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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