C++ 中的 :: 是什么意思?

发布于 2024-09-14 10:25:20 字数 263 浏览 3 评论 0原文

void weight_data::rev_seq(string &seq){ 
//TODO
std::reverse(seq.begin(), seq.end());
}

在这个C++方法中,我认为这个方法不会返回任何东西,所以前缀是void::告诉了weight_data之间的关系和rev_seq(string &seq)?谢谢!

void weight_data::rev_seq(string &seq){ 
//TODO
std::reverse(seq.begin(), seq.end());
}

In this C++ method, I think this method does not return anything, so the prefix is void, what does :: tell the relationships between weight_data and rev_seq(string &seq)? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瞎闹 2024-09-21 10:25:20

void 是返回类型。 :: 是范围解析运算符,因此意味着 rev_seqweight_data 范围内。 weight_data 可以是命名空间或类(根据您给出的内容,无法说出是哪个)。

void is the return type. :: is the scope resolution operator, so it means rev_seq is inside the scope of weight_data. weight_data could be either a namespace or a class (based on what you've given, it's not possible to say which).

上课铃就是安魂曲 2024-09-21 10:25:20

在 C++ 中,

  • A::B 表示 Bnamespaceclass 类型 A
  • AB 表示 Bstruct 的成员, classunion 类型,其实例由对象或引用 A 引用,并且
  • A-> ;B 表示 Bstructclassunion 的成员> 键入一个由指针A 引用的实例。 (它相当于 (*A).B。)

在某些其他语言中,所有三种情况都仅由 . 涵盖。

请注意,在 C++ 中,成员函数不必在其类定义中实现(定义)。 (如果是,则它们是隐式内联。)它们可以而且通常在单独的实现 (.cpp) 文件中实现。这样做的优点是,当您更改类的成员函数之一的实现时,并非该类的所有用户都需要重新编译。因此,除非 weight_data 是一个 namespace 名称,void Weight_data::rev_seq(string &seq) {...} 就是这样的定义班级之外的班级成员。

In C++,

  • A::B means B is an identifier within either namespace or class type A,
  • A.B means B is a member of the struct, class, or union type an instance of which is referred to by the object or reference A, and
  • A->B means B is a member of the struct, class, or union type an instance of which is referred to by the pointer A. (It's equivalent to (*A).B.)

In some other languages, all three cases are covered by a . only.

Note that in C++, member function don't have to be implemented (defined) within their class' definition. (If they are, they are implicitly inline.) They can be, and often are, implemented in separate implementation (.cpp) files. This has the advantage that not all users of a class need to recompile when you change an implementation of one of the class' member functions. So unless weight_data is a namespace name, void weight_data::rev_seq(string &seq) {...} is such a definition of a class member outside of its class.

我偏爱纯白色 2024-09-21 10:25:20

weight_data 是一个 命名空间 或类名。

weight_data is a namespace or class name.

星軌x 2024-09-21 10:25:20

void Weight_data::rev_seq(string &seq) 行告诉编译器这是 rev_seq(string &seq) 成员函数的定义>权重数据。如果这只是说 void rev_seq(string &seq) { ... } 编译器会认为正在定义一个非成员函数,而不是 rev_seq(string & seq)weight_data 类的成员函数。

class weight_data
{
    void rev_str(string &seq);
}

它还可能意味着 rev_str 引用属于命名空间 weight_data 一部分的函数。

namespace weight_data
{
    void rev_str(string &seq);
}

The line void weight_data::rev_seq(string &seq) tells the compiler that this is the definition of the rev_seq(string &seq) member function from the weight_data. If this just said void rev_seq(string &seq) { ... } the compiler would think that a non-member function was being defined, as opposed to the rev_seq(string &seq) member function of the weight_data class.

class weight_data
{
    void rev_str(string &seq);
}

It can also mean that rev_str refers to a function that is part of namespace weight_data.

namespace weight_data
{
    void rev_str(string &seq);
}
倚栏听风 2024-09-21 10:25:20

只是想添加关于 ::

a) Operator :: 的

struct A{
   int m;
};

int x;
int main(){
   ::x;                     // Unary
   int (A::*p) = &A::m;     // Binary
}

2 个更有趣的事情b) $10.3/12 - “使用范围运算符 (5.1) 的显式限定会抑制虚拟调用机制。”

struct A{
   virtual void f(){cout << 1;}
};

struct B : A{
   void f(){cout << 2;}
};

int x;
int main(){
   B b;
   A &ra = b;
   ra.f();     // dynamic binding, prints 2
   ra.A::f();  // suppress VF mechanism, prints 1.
}

Just thought of adding 2 more interesting things about the ::

a) Operator :: is both a unary and a binary operator

struct A{
   int m;
};

int x;
int main(){
   ::x;                     // Unary
   int (A::*p) = &A::m;     // Binary
}

b) $10.3/12 - "Explicit qualification with the scope operator (5.1) suppresses the virtual call mechanism."

struct A{
   virtual void f(){cout << 1;}
};

struct B : A{
   void f(){cout << 2;}
};

int x;
int main(){
   B b;
   A &ra = b;
   ra.f();     // dynamic binding, prints 2
   ra.A::f();  // suppress VF mechanism, prints 1.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文