c# - c++ - using std namespace and dependancies -


while trying familiarize myself c++ , concepts, came across

using namespace std 

and

#include <iostream> 

my simple code follows

#include "stdafx.h" #include "consoleapplication5.h" #include <iostream>       int main() {     std::cout << "hi";     return 0; } 

using visual studio 2015 community, uses intellisense, shows

cout

uses following

std::ostream std::cout

as c# programmer, confuses me slightly. :

std::ostream

being return type while

std::cout

being method/parameter passed or

std::ostream

a dependancy of

cout

update (after archimaredes answer)

in c#, 1 can use following:

streamwriter srwrite; 

or

streamwriter srwrite = new streamwriter(string filepath) 

or 1 can use:

streamwriter srwrite = new streamwriter(new networkstream(socket.getstream())); 

each case object of type namely

streamwriter 

is assignable new object or existing file or networkstream.

i tried same after mentioned (excuse c# mentality) std::ostream x = new std:ostream returns no default constructor.

could add how std::ostream , std::cout relate each other, creates/initalizes other. concept still bit vague me.

std::cout object in global scope, of type std::ostream. when call std::cout << "hi", invoking operator<<() method std::cout object left-hand value , string literal "hi" right-hand value.

cout , ostream inside std namespace, hence std:: prefix. if place using namespace std in code, allows omit prefixes, e.g.

#include <iostream> using namespace std;  int main() {     cout << "hi"; // no error because 'std' unnecessary } 

namespaces used prevent name conflicts, in same way c#; practice not use using namespace directives entire source code files in order prevent name conflicts in code.


edit in response op's update

std::cout instance of std::ostream. illustrate this, consider following:

class a_class { public:     a_class() {}     void foo() {} };  int main() {     a_class an_instance;     an_instance.foo(); } 

a_class class, while an_instance instance of a_class.

similarly; ostream class, while cout instance of ostream.


edit in response op's comments

this may confusing user of c#, exact same concept as:

int n = 5; 

whereint type, , n variable name.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -