Solved

What's The Difference.....

Posted on 2004-09-07
3
192 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
Comment Utility
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
Comment Utility
Forgot to mention that "std" is the namespace name for STL library.
0
 
LVL 30

Accepted Solution

by:
Axter earned 25 total points
Comment Utility
>>....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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 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

11 Experts available now in Live!

Get 1:1 Help Now