简体   繁体   中英

:: (scope resolution operator) is used for multiple purposes in C++?

I am learning C++, and want to confirm few thins. I have seen that std::cin has :: operator which means that iostream.h file must have cin related code inside namespace std . Whereas if you include myclass.h and have class NOT inside namespace like this

//myclass.h
class x { void func(){}};

Then you don't need to do this myclass:x.func(); Instead i can do just this x.func(); . Correct Becuase it's not inside namespace.

Secondly, :: can be used to define class functions outside of the class like in their cpp file. Correct?

Now my additional question is that are these two functions separate or one? do they just look different to me?

:: (scope resolution operator) is used for multiple purposes in C++?

It's only really serving one purpose - to help find the namespace and/or class/struct/union scope that an identifier's in.

The compiler knows to look first in the tightest scope then work its way back to the global scope, but it also considers any namespaces and identifiers you're "using" (eg using namespace xxx; or using ns1::identifier; ).

Then you don't need to do this myclass:x.func(); Instead i can do just this x.func();

It has nothing to do with files on disk.

Secondly, :: can be used to define class functions outside of the class like in their cpp file. Correct?

Yes. When you define a member function outside its class, you use the class name so it's not deemed a distinct non-member function in the current scope.

Now my additional question is that are these two functions separate or one? do they just look different to me?

Just the one in the defining-member-function scenario above.

I have seen that std::cin has :: operator which means that iostream.h file must have cin related code inside namespace std.

I realize this isn't a question, but you said iostream.h and I wanted to point out that you should not use iostream.h because it is deprecated, use iostream instead.

Then you don't need to do this myclass:x.func(); Instead i can do just this x.func();. Correct Becuase it's not inside namespace.

This is correct. You could do the same thing if all of your code was inside a namespace rather than a class.

Secondly, :: can be used to define class functions outside of the class like in their cpp file. Correct?

Correct, so long as they have been declared, but not defined, in the class scope this is allowed and usually considered preferable for readability.

Now my additional question is that are these two functions separate or one? do they just look different to me?

If both are in the class scope already, void x::func(){} and void func(){} result in the same thing (one would call both by using xa; a.func() , for example).

EDIT: In response to the comment, you can define a namespace basically the same way you define a class, as:

namespace funcs
{
    void innerfunc(){}
    void outerfunc();
    void funcs::scopedfunc(){}
}
funcs::outerfunc();

As you can infer from std , these are called as free functions, rather than being called from an object like a non-static class function.

int main()
{
    funcs::innerfunc();
    using namespace funcs;
    outerfunc();
}

Then you don't need to do this myclass:x.func(); Instead i can do just this x.func();.

Note that neither of these will compile. You cannot use the dot operator with a class; you can only use it with an object. Specifically, myObject.func(); serves an entirely different purpose than MyClass::func() { } . The first calls the member function func() on the object named myObject . The later is used to define the function func() which belongs to the scope of MyClass .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM