简体   繁体   English

:: 与 。 用于成员函数(C ++)

[英]:: versus . for member functions (C++)

I know that, given a class, say, std::array, which has a member function, say, size(), we can call on that member function via a ".", that is, in the following code, 我知道,给定一个类,例如std :: array,它具有一个成员函数,例如size(),我们可以通过“。”调用该成员函数,也就是说,在以下代码中,

array<int,5> myarray;
int s=myarray.size();

s would be the integer representing the size of myarray. s是代表myarray大小的整数。 The tricky thing happens when member functions can also be called by the namespace operator "::". 棘手的事情发生在名称空间运算符“ ::”也可以调用成员函数时。 For example, I know that the following line of code is valid: 例如,我知道以下代码行是有效的:

auto t=chrono::high_resolution_clock::now();

Then, what's wrong with employing the syntax we originally used with array? 然后,采用我们最初用于数组的语法有什么问题?

chrono::high_resolution_clock myclock;
auto t=myclock.now();

now() is a static member function. now()static成员函数。 This means that the function itself doesn't have a hidden this pointer. 这意味着函数本身没有隐藏的this指针。 Instead, it's just like a regular function - just part of the class to avoid name collisions. 相反,它就像一个常规函数-只是类的一部分,以避免名称冲突。

(THe high_resolution_clock is a class, chrono is a namespace, in your example. Both use :: to indicate "I want something from inside {namespace, class}) (在您的示例中, high_resolution_clock是一个类, chrono是一个名称空间。都使用::表示“我想要{namespace,class}内部的东西)

In simple terms, :: separates names from surnames, while . 简单来说, ::将名字和姓氏分开,而. separates components from sub-components. 将组件与子组件分离。 (Note that in many languages like C#, Java, D, ...) there is no such distinction) (请注意,在许多语言中,例如C#,Java,D等),没有这种区别)

In your fist example, myarray is a variable, whose size() method refer to that particular variable. 在您的第一个示例中, myarray是一个变量,其size()方法引用该特定变量。

array<int,5> myarray_a, myarray_b;
int sa=myarray_a.size();
int sb=myarray_b.size();

Will give the sizes of myarray_a and b respectively (not of array<int,5> , even if -due to this particular case- all sizes will be 5) 将分别给出myarray_ab的大小(而不是array<int,5> ,即使由于这种特殊情况,所有大小均为5)

In the second example, now() is a static method of the class chrono::high_resolution_clock . 在第二个示例中,now()是chrono::high_resolution_clock类的static方法。

It doesn't matter if you have or not a variable (and how many) of type chrono::high_resolution_clock . 是否具有chrono::high_resolution_clock类型的变量(以及数量)无关紧要。 That function does not refer to the variable but works the same for all variables of the same type (there is conceptually just one now , no matter who you ask to). 该函数不引用变量,但是对相同类型的所有变量都起作用(概念上now只有一个,无论您问谁)。

Because of this, call now() as part of a variable, of by fully qualifying its name is the same. 因此,通过完全限定其名称来调用now()作为变量的一部分是相同的。

Note that, the size() function of std::array is strange : the size of std::array is compile time defined, hence size() could have been static as well. 需要注意的是,该size()的函数std::array奇怪 :的大小std::array是编译定义的时间,因此, size()本来是静态为好。 But std:: designers let them as member (although constexpr , so still usable in compile time expressions) to retain the same behavior as in std::array or other containers (where it has to be dynamic, and associated to a variable, since each size can vary during execution) 但是std::设计器允许它们作为成员(尽管constexpr ,因此仍可在编译时表达式中使用)以保留与std::array或其他容器(必须是动态的并与变量关联的容器)相同的行为,因为每个大小在执行期间可能会有所不同)

您在这里混淆了两个概念:“ ::”用于名称空间,也用于调用静态方法。

There is nothing wrong with the syntax you suggested. 您建议的语法没有 It works. 有用。

However it creates an object, whereas the :: version does not create any object. 但是,它将创建一个对象,而::版本不会创建任何对象。 There doesn't seem to be much point in creating that object since it is not necessary to do so in order to call the static function. 创建该对象似乎没有多大意义,因为不必为了调用静态函数而这样做。 So it is simpler to just call the static function without creating the object. 因此,仅调用静态函数而不创建对象会更简单。

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

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