While trying to familiarize myself with c++ and its concepts, I came across the
using namespace std
and
#include <iostream>
my simple code is as follows
#include "stdafx.h"
#include "ConsoleApplication5.h"
#include <iostream>
int main()
{
std::cout << "hi";
return 0;
}
Using Visual Studio 2015 Community, which uses intellisense, shows
cout
uses the following
std::ostream std::cout
As a C# programmer, this confuses me slightly. Is this :
std::ostream
being a return type while
std::cout
being the method/parameter passed or is
std::ostream
a dependancy of
cout
UPDATE (after Archimaredes Answer)
In C#, one can use the following:
StreamWriter srWrite;
or
StreamWriter srWrite = new StreamWriter(string filepath)
or one can use:
StreamWriter srWrite = new StreamWriter(new NetworkStream(Socket.GetStream()));
Each case a object of type namely
StreamWriter
is assignable with a new object or a existing file or a networkstream.
I tried the same after that which you mentioned (excuse the C# mentality) with the std::ostream x = new std:ostream
which returns a no default constructor.
Could you just add how std::ostream and std::cout relate to each other, which creates/initalizes the other. THis concept is still a bit vague to me.
“std” is an abbreviation for standard. So that means we use all the things with in “std” namespace. If we don't want to use this line of code, we can use the things in this namespace like this. std::cout, std::endl.
To put it as an advice: Do not use "using namespace" (std or other) at file scope in header files. It is OK to use it in implementation files.
In C++, a namespace is a collection of related names or identifiers (functions, class, variables) which helps to separate these identifiers from similar identifiers in other namespaces or the global namespace. The identifiers of the C++ standard library are defined in a namespace called std .
You should definitely NOT use using namespace in headers for precisely the reason you say, that it can unexpectedly change the meaning of code in any other files that include that header. There's no way to undo a using namespace which is another reason it's so dangerous.
std::cout
is an object in global scope, of type std::ostream
. When you call std::cout << "hi"
, you are invoking the operator<<()
method with the std::cout
object as your left-hand value and the string literal "hi"
as the right-hand value.
cout
and ostream
are inside the std
namespace, hence the std::
prefix. If you place using namespace std
in your code, this allows you to omit the prefixes, e.g.
#include <iostream>
using namespace std;
int main() {
cout << "hi"; // no error because the 'std' is unnecessary now
}
Namespaces are used to prevent name conflicts, in exactly the same way as C#; it is usually good practice to not use using namespace
directives for entire source code files in order to prevent name conflicts in your code.
Edit in response to OP's update
std::cout
is an instance of std::ostream
. To illustrate this, consider the following:
class A_Class {
public:
A_Class() {}
void foo() {}
};
int main() {
A_Class an_instance;
an_instance.foo();
}
A_Class
is a class, while an_instance
is an instance of A_Class
.
Similarly; ostream
is a class, while cout
is an instance of ostream
.
Edit in response to OP's comments
This may look confusing to a user of C#, but it is the exact same concept as:
int n = 5;
Whereint
is the type, and n
is the variable name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With