Solved

What's The Difference.....

Posted on 2004-09-07
3
204 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

912 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now