简体   繁体   English

::(作用域分辨率运算符)在C ++中有多种用途?

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

I am learning C++, and want to confirm few thins. 我正在学习C ++,并且想确认一些知识。 I have seen that std::cin has :: operator which means that iostream.h file must have cin related code inside namespace std . 我已经看到std::cin具有::运算符,这意味着iostream.h文件必须在命名空间std中具有与cin相关的代码。 Whereas if you include myclass.h and have class NOT inside namespace like this 而如果您包含myclass.h并且在这样的命名空间中没有类,

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

Then you don't need to do this myclass:x.func(); 然后,您无需执行此myclass:x.func(); Instead i can do just this x.func(); 相反,我只能执行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. 其次, ::可以用于在类外部定义类函数,例如在其cpp文件中。 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++? ::(作用域分辨率运算符)在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; ). 编译器知道首先要在最紧密的范围内查看,然后再回到全局范围,但是它还会考虑您正在“使用”的任何名称空间和标识符(例如, using namespace xxx;using ns1::identifier; )。

Then you don't need to do this myclass:x.func(); 然后,您无需执行此myclass:x.func();。 Instead i can do just this x.func(); 相反,我只能执行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. 其次,::可以用于在类外部定义类函数,例如在其cpp文件中。 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. 我已经看到std :: cin具有::运算符,这意味着iostream.h文件必须在命名空间std中包含与cin相关的代码。

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. 我意识到这不是问题,但您说的是iostream.h ,我想指出的是您不应该使用iostream.h因为它已被弃用,而应使用iostream

Then you don't need to do this myclass:x.func(); 然后,您无需执行此myclass:x.func();。 Instead i can do just this x.func();. 相反,我只能做这个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. 其次,::可以用于在类外部定义类函数,例如在其cpp文件中。 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). 如果两者都已经在类范围内,则void x::func(){}void func(){}产生相同的结果xa; a.func()例如,可以使用xa; a.func()来调用两者)。

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. 正如您可以从std推断出的那样,这些被称为自由函数,而不是像非静态类函数那样从对象中调用。

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

Then you don't need to do this myclass:x.func(); 然后,您无需执行此myclass:x.func();。 Instead i can do just this x.func();. 相反,我只能做这个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(); 具体来说, myObject.func(); serves an entirely different purpose than MyClass::func() { } . MyClass::func() { }用途完全不同。 The first calls the member function func() on the object named myObject . 第一个调用名为myObject的对象的成员函数func() The later is used to define the function func() which belongs to the scope of MyClass . 后者用于定义函数func() ,该函数属于MyClass的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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