I am learning C++ and I can never tell when I need to use ::
. I do know that I need to use std::
in front of cout
and cin
. Does this mean that inside of the iostream
file the developers that created it made a namespace called std
and put the functions cin
and cout
into the namespace called std
? When I created a new class that isn't in the same file as main()
for some reason I must add ::
.
For example, if I create a class
called A
, why do I need to put A::
in front of a function that I make, even though I didn't put it into a namesace? For example void A::printStuff(){}
. If I create a function in main
, why don't I have to put main::printStuf{}
?
I know that my question is probably confusing, but could someone help me?
In C++, scope resolution operator is ::. It is used for following purposes. 1) To access a global variable when there is a local variable with same name: // C++ program to show that we can access a global variable. // using scope resolution operator :: when there is a local.
The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the class and used to access the static variables of class.
:: is the scope resolution operator. std::cout is the name cout in the namespace std .
The :: operator is known as the scope resolution operator, and it is used to get from a namespace or class to one of its members. The . and -> operators are for accessing an object instance's members, and only comes into play after creating an object instance. You use .
You're pretty much right about cout
and cin
. They are objects (not functions) defined inside the std
namespace. Here are their declarations as defined by the C++ standard:
Header
<iostream>
synopsis#include <ios> #include <streambuf> #include <istream> #include <ostream> namespace std { extern istream cin; extern ostream cout; extern ostream cerr; extern ostream clog; extern wistream wcin; extern wostream wcout; extern wostream wcerr; extern wostream wclog; }
::
is known as the scope resolution operator. The names cout
and cin
are defined within std
, so we have to qualify their names with std::
.
Classes behave a little like namespaces in that the names declared inside the class belong to the class. For example:
class foo { public: foo(); void bar(); };
The constructor named foo
is a member of the class named foo
. They have the same name because its the constructor. The function bar
is also a member of foo
.
Because they are members of foo
, when referring to them from outside the class, we have to qualify their names. After all, they belong to that class. So if you're going to define the constructor and bar
outside the class, you need to do it like so:
foo::foo() { // Implement the constructor } void foo::bar() { // Implement bar }
This is because they are being defined outside the class. If you had not put the foo::
qualification on the names, you would be defining some new functions in the global scope, rather than as members of foo
. For example, this is entirely different bar
:
void bar() { // Implement different bar }
It's allowed to have the same name as the function in the foo
class because it's in a different scope. This bar
is in the global scope, whereas the other bar
belonged to the foo
class.
The ::
are used to dereference scopes.
const int x = 5; namespace foo { const int x = 0; } int bar() { int x = 1; return x; } struct Meh { static const int x = 2; } int main() { std::cout << x; // => 5 { int x = 4; std::cout << x; // => 4 std::cout << ::x; // => 5, this one looks for x outside the current scope } std::cout << Meh::x; // => 2, use the definition of x inside the scope of Meh std::cout << foo::x; // => 0, use the definition of x inside foo std::cout << bar(); // => 1, use the definition of x inside bar (returned by bar) }
unrelated: cout and cin are not functions, but instances of stream objects.
EDIT fixed as Keine Lust suggested
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