Thursday, 15 January 2015

function - Using :: in C++ -


I am learning C ++ and when I need to use :: I can never tell I know that I need to use in front of std :: cout and cin . I mean that Creating a file within iostream created a namespace named std and put the function cin and cout std in the name space when I became a new class Or who is not in the main () as a single file for any reason, then I :: will add.

For example, if I make a class to A , then why should I see that code in front of a :: I have to put it, which I do not have put it in names? For example zero A: printStuff () {} . If I can make a function in main , then I have to put main :: printStuf {}

I know that my question is Maybe misleading, but can someone help me?

You click on cout and cin . They are defined objects (not functions) in the std namespace C ++ standard:

header & lt; Iostream & gt; summary

  #include & lt; IOS & gt; These are their announcements as defined by. # Include & lt; Streambuf & gt; # Include & lt; Istream & gt; # Include & lt; Ostream & gt; Namespace study {extern istream cin; Exstern Australia Coat; Exxon ostream care; Prevent External Ostream; Externwistrume WCCN; Externist whistle wcout; Extortion whistle wcerr; External Vertical Wall Clog; }    

:: is known as scope resolution operator name cout And cin are defined in std , so we need to have our code qualified with std :: .

Classes behave similar to those little names, which are related to class named Names inside the class. For example:

  class foo {public: foo (); Zero times (); }; The named creator of   

foo is the class named subscriber foo has the same name as its constructor function Bar is also a member of foo .

Because they are members of foo , when they give context to them outside the classroom, we have to qualify our names after all, they belong to that class, so if you If you are going to define outside constructor and bar class, then you should do this:

  foo: foo () {// implement constructor} void The reason for this is that the class is being defined as    If you did not qualify for the  foo:  name, you would define some new functions in the global area, instead of a member of  foo . For example, it is completely different  bar :  
  zero times () {// apply different times}   < P> This permission is to keep the same name as the function in the  foo  class because it is in a different radius, it is in the  bar  global scope, while the other  times < Related to / code>  foo  category.   

No comments:

Post a Comment