Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope resolution operator being used twice

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));
}

I just encountered this function definition on GitHub.

I assume that the second and the third "Commitment" refer to the class name and constructor, but I can't figure out the meaning of the first. I am sure that it does not refer to the namespace because that name is different. I have seen the scope resolution operator being used twice in examples, but those refer to nested namespaces.

like image 258
Eloy Avatar asked Nov 30 '17 13:11

Eloy


People also ask

Is scope resolution operator is overloaded?

No. There are C++ operators that can't be overloaded. They include: :: -Scope resolution operator.

What is double colon used for in C++?

Two colons (::) are used in C++ as a scope resolution operator. This operator gives you more freedom in naming your variables by letting you distinguish between variables with the same name.

What is the scope resolution operator explain its use?

The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class.

What does double colon mean in PHP?

In PHP, the double colon :: is defined as Scope Resolution Operator. It used when when we want to access constants, properties and methods defined at class level. When referring to these items outside class definition, name of class is used along with scope resolution operator.


1 Answers

In C++ classes have the feature of having their name injected into their scope ([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.

And the code snippet you showed makes use of it. In certain contexts Commitment::Commitment names the class itself, and in others names the c'tor. Only the last Commitment(, where you open the parentheses, begins the c'tor definition.

And it can look much much worse:

struct foo {
    foo();
};

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

Which you can see is valid C++ Live.

like image 81
StoryTeller - Unslander Monica Avatar answered Oct 22 '22 12:10

StoryTeller - Unslander Monica