Scope Resolution Operator Being Used Twice

Scope resolution operator being used twice

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.

Scope resolution operator for returning a nested class type

  1. The class scope is needed for the function return type because name lookup won't yet know of it.

You can get around that requirement if you wish with C++11 trailing function return type.

auto UserInformation::getInfo(int userId) -> UserInfo
{ ... }

  1. The class scope is not needed inside the member function body because there the class scope is then obviously known.

:: (scope resolution operator) is used for multiple purposes in C++?

I have seen that std::cin has :: operator which means that iostream.h file must have cin related code inside namespace std.

I realize this isn't a question, but you said iostream.h and I wanted to point out that you should not use iostream.h because it is deprecated, use iostream instead.

Then you don't need to do this myclass:x.func(); Instead i can do just this x.func();. Correct Becuase it's not inside namespace.

This is correct. You could do the same thing if all of your code was inside a namespace rather than a class.

Secondly, :: can be used to define class functions outside of the class like in their cpp file. Correct?

Correct, so long as they have been declared, but not defined, in the class scope this is allowed and usually considered preferable for readability.

Now my additional question is that are these two functions separate or one? do they just look different to me?

If both are in the class scope already, void x::func(){} and void func(){} result in the same thing (one would call both by using x a; a.func(), for example).

EDIT: In response to the comment, you can define a namespace basically the same way you define a class, as:

namespace funcs
{
void innerfunc(){}
void outerfunc();
void funcs::scopedfunc(){}
}
funcs::outerfunc();

As you can infer from std, these are called as free functions, rather than being called from an object like a non-static class function.

int main()
{
funcs::innerfunc();
using namespace funcs;
outerfunc();
}

Why i can't use scope resolution operator during the constructor initialization?

The member-initialisation list is more constrained than some arbitrary expression.

While it is true that you can qualify member names, like Vector::sz, in expressions generally (where sz is in scope), the syntax of a part of the member-initialisation list simply does not support this. You must write name(init) or name{init}; nothing else.

C++ could have been made to allow qualified names here, but there would have been absolutely no point in doing so, while making the parser more complex.

Fortunately, none of us actually needs to worry about this in any way whatsoever.

tl;dr: Because.

Confusion about use of scope resolution operator in STL

In both cases, the qualified name is used to name a member of the class template specialization. This can be used to name a static class member. Also, a typedef or using alias can appear in a class definition or class template definition, and the type alias name is considered a member of the class.

std::tuple_size<tupleType>::value is a static member (which specifies the number of elements in the tuple type). std::vector<int>::iterator is a member class type (and objects of that type can be used to iterate over the vector's elements).

For example:

template <typename T> class A
{
public:
static const int value = 3;
typedef int number_type;
using param_type = T;
using container_type = std::vector<T>;
};

int main() {
int a = A<int>::value; // a is initialized to 3
A<int>::number_type n = 0; // n is an int
A<int>::param_type p = 1; // p is an int
A<double>::param_type q = 2.5; // q is a double
A<double>::container_type v; // v is a std::vector<double>
}

(As the example shows, a member of a class template can depend on template arguments.)

Why does the code work after adding scope resolution parameter?

It's impossible to tell for sure without the actual error message, but it looks like the problem is using namespace std;.
This brings in the function std::distance() which isn't what you want, but using the scope operator to request the global distance() function works again.

Avoid bringing in the entire std namespace.



Related Topics



Leave a reply



Submit