c++ - 范围解析运算符被使用两次

标签 c++ class constructor namespaces name-lookup

namespace libzerocoin {

//Commitment class
Commitment::Commitment::Commitment(const IntegerGroupParams* p,
                               const Bignum& value): params(p), contents(value) {
this->randomness = Bignum::randBignum(params->groupOrder);
this->commitmentValue = (params->g.pow_mod(this->contents, params->modulus).mul_mod(
                         params->h.pow_mod(this->randomness, params->modulus), params->modulus));
}

我刚刚在 GitHub 上遇到这个函数定义.

我假设第二个和第三个“ promise ”是指类名和构造函数,但我无法弄清楚第一个的含义。我确信它没有引用 namespace ,因为该名称不同。我在示例中看到范围解析运算符被使用了两次,但那些引用了嵌套的命名空间。

最佳答案

在 C++ 中,类具有将其名称注入(inject)其作用域的特性 ([class]/2):

The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.

您展示的代码片段使用了它。在某些情况下,Commitment::Commitment 命名类本身,而在其他情况下命名 c'tor。只有最后一个 Commitment(,您打开括号的地方,开始 c'tor 定义。

它看起来更糟:

struct foo {
    foo();
};

foo::foo::foo::foo() = default;

您可以看到这是有效的 C++ Live .

关于c++ - 范围解析运算符被使用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51597003/

相关文章:

c++ - 无向强连通图的所有简单路径

c++ - type_info是如何实现的

c++ - 为什么 C++11 会从 std::vector 的填充构造函数原型(prototype)中移除默认值?

class - Haskell 自定义数学类型和类

c++ - 使用其他对象的类构造函数

c++ - 当第二个依赖于第一个时,如何在构造函数初始值设定项列表中初始化两个 std::array

scala - 在主构造函数中初始化时重新分配给 val

C++友元类数据访问

如果两个 div 具有相同的类,则 javascript 会触发

python - 如何在 Python 2.7 中复制类及其列表成员而不复制引用?