Why doesn't everyone start using (unless you have an older compiler) the:
#include <iostream> //notice no .h
using namespace std;
?
Was just wondering if their is a reason between the two.
Printable View
Why doesn't everyone start using (unless you have an older compiler) the:
#include <iostream> //notice no .h
using namespace std;
?
Was just wondering if their is a reason between the two.
It's a filename, it doesn't need to be .h it could be .banana if you want
That would be cool in a weird kind of way. If I ever write a language, header files will be .banana :)
The reason most people don't use the STL versions is because:
a) it's a new(er) style of syntax that old tutorials/books don't mention
b) anyone using M$ VC++ will see the .h included in any help file examples, since that will use M$'s header files (slightly different from the ANSI ones).
Yeah, Harry, and source files could be .monkey =P.
Z.
I just bought a C++ book, and it states that using the ANSI standard versions (no .h) and namespace std has very strong debugging and OOP differences. The ANSI version is actually the OOP version.
I was taught to use the <iostream.h> syntax in University... (which was 4 years ago.)
What's the difference with using just <iostream>? Is there any specific ways you have to call functions using this? Benefits? Downsides? And what does std do?
Destined
(A guy who switched his majors from Computer Science to Physics, 4 years ago... :p)
For example, there is a major difference between <string.h> and <string>.
The first one is the C include, which contains functions for string (char array) manipulation.
The second one is actually a C++ string class + the functions from the C include file.
The old .h style header includes got deprecated a while back, but AFAIK it was standard to use .h style includes in C++ up until then. I have no idea when the governing bodies of C++ decided to drop the .h, but it could have been less than 4 years ago I guess.
I was taught to use .h too, but I was being taught C, not C++.
std is the standard namespace. When you include STL headers, their contents go into the std namespace (I think that goes for all STL headers, but I'm not sure). Namespaces are very useful to prevent naming conflicts by placing things in their own custom scopes. The using directive can place you in a different namespace to the default namespace (the global namespace), which can ease your typing by preventing you from having to fully qualify every variable and function from that namespace you use with the scope.
So, without the using directive you would type:
std::cout
but with it you could write just:
cout
Anyway it's good practice to use the non-deprecated versions of things, so use the new style.
And as a qualification, you should never use using namespace whatever; in a header file, because that can really mess things up as it applies it to every source file it's included in. The using directive only otherwise applies to the current source file.