简体   繁体   English

关于 C++ 中的 Scope 解析运算符的问题?

[英]question about Scope Resolution Operator in C++?

I am learning C++ and not quite sure about the Scope Resolution Operator.我正在学习 C++ 并且不太确定 Scope 分辨率运算符。 suppose I have the following code: code 1:假设我有以下代码:代码1:

class Student {
     int no;
     int semester;
     char grade[M+1];
 public:
     void display() const;
 };

 void Student::display() const {
     cout << "Hi!" << endl;
 }

code 2:代码2:

class Student {
     int no;
     int semester;
     char grade[M+1];
 public:
     void display() const{
        cout << "Hi!" << endl;
    }
 };

any differences if I define the display() inside its class?如果我在它的 class 中定义 display() 有什么不同吗? if there is no difference, then why should I use the scope resolution operator?如果没有区别,那我为什么要使用scope分辨率算子呢?

There is one difference - if defined in the class the function behaves as if it were declared as inline .有一个区别 - 如果在 class 中定义,则 function 的行为就像它被声明为inline一样。 But this is no big deal, and the compiler may well ignore it.但这没什么大不了的,编译器很可能会忽略它。 The big difference is that for large, multi-file projects, if you make a change to the function body defined in the header, EVERY other file that uses your header will have to be recompiled.最大的区别在于,对于大型的多文件项目,如果您对 header 中定义的 function 主体进行更改,则使用您的 Z099FB995346F31C749F6E40DB0F3 的每个其他文件都将重新编译为。 Whereas, if you make a change to the function body defined in a.cpp file, then only that file needs to be recompiled.然而,如果您对 a.cpp 文件中定义的 function 主体进行更改,则只需重新编译该文件。 This can and does make a huge difference to real-world projects.这可以而且确实对现实世界的项目产生了巨大的影响。

The scope resolution operator allows you to define the function outside the class. scope 分辨率运算符允许您在 class 之外定义 function。
This provides the ability to not expose/abstract your source code implementation from the users of your library.这提供了不向库的用户公开/抽象您的源代码实现的能力。 The class declarations are placed in header files and the actual definitions are kept in a cpp file and then only the object files or libraries of the same are provided to the end user. class 声明放在 header 文件中,实际定义保存在 cpp 文件中,然后只向最终用户提供 object 文件或相同的库。
You can say it provides oneway of protecting intellectual rights though the feature was not designed specifically for it.您可以说它提供了一种保护知识产权的方式,尽管该功能并不是专门为它设计的。

Normally you would define the class in the header file and then implement it in the.cpp file.通常你会在 header 文件中定义 class 然后在 .cpp 文件中实现它。 For the compiler to know what function belongs to which class, you use the Scope Resolution Operator, ie :: .为了让编译器知道 function 属于哪个 class,您使用 Scope 解析运算符,即::

When you place the function directly inside your header like you showed in example two, you need to prefix it with the inline keyword, which tells the compiler to put that entire function inside the code where you are calling it.当您将 function 直接放在 header 中时,如示例二所示,您需要在其前面加上inline关键字,这会告诉编译器将整个 ZC1C425268E68385D1AB5074C17A94 代码放入其中。 Normally you do this with very small functions and for speed considerations, using it a lot can lead to code bloat in your exe.通常,您使用非常小的函数来执行此操作,并且出于速度考虑,大量使用它会导致 exe 中的代码膨胀。

The main reason is that you don't want to clutter up the class definition with all sorts of irrelevant implementation details, like the implementation of a function.主要原因是您不想将 class 定义与各种不相关的实现细节混在一起,例如 function 的实现。 When the class is small, with only one or two functions, and the functions are small, with only one or two lines, it's not necessarily a problem, but in real life, putting the function definitions in the class itself very quickly leads to unreadable code;当 class 很小,只有一两个函数,而函数很小,只有一两行时,不一定有问题,但在现实生活中,将 function 定义放在 ZA2F2ED4F8EBC06AB1DZ 本身很快就会导致4不可读代码; you want to keep the external definition separate from the internal implementation as much as possible.您希望尽可能将外部定义与内部实现分开。 (On large projects, they'll typically be maintained by different people anyway.) (在大型项目中,它们通常由不同的人维护。)

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

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