Questions tagged [scope-resolution-operator]

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.

For example:

int count = 0;

int main(void) {
   int count = 0;
   ::count = 1;  // set global count to 1
   count = 2;    // set local count to 2
   return 0;
}

The declaration of count declared in the main function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope.

21 questions
14
votes
3 answers

Is a fully qualified class name down to global scope ever required for out-of-line member function definitions?

This question got me wondering whether it is ever useful/necessary to fully qualify class names (including the global scope operator) in an out-of-class member function definition. On the one hand, I've never seen this done before (and the syntax to…
5
votes
1 answer

What is the difference between calling foo() and ::foo() within a C++ class member function?

I am looking at someone else's C++ code (note I am not fluent in C++). Within the class, there is this member function: void ClassYaba::funcname() { ... ::foo(); ... } There is no member function within that class's namespace named foo,…
Ed.
  • 928
  • 1
  • 10
  • 23
5
votes
2 answers

use of + followed by :: (scope resolution operator in C++ code

I have following C++ code snippet : inline std::vector> cloneTensorBuffer( const std::vector& tensors) { auto ret = std::vector>{}; auto type =…
Sunil Puranik
  • 87
  • 1
  • 4
4
votes
6 answers

How can i access global variable using function in c++?

I want to access the value assigned to global variable in main function from the function. I don't want to pass argument in function. I have tried referring different stack overflow similar questions and C++ libraries . #include long s; …
sahil arora
  • 106
  • 1
  • 10
3
votes
0 answers

Using scope resolution operator to define mutiple class member function

Just for convenience, is there a way to turn this class foo{ public: void fun1(); void fun2(); } void foo::fun1(){ //something } void foo::fun2(){ //something } into something like this? (I know the code is invalid) class foo{ …
jteng2127
  • 63
  • 1
  • 5
3
votes
2 answers

Scope operator in Operator Overloading

I'm not able to understand the scope operator in Operator overloading. There are examples when they are using it when they don't. When I'm supposed to write T::operator. Can I write just the operator still works fine or is it recommended to…
Hunali
  • 277
  • 1
  • 11
1
vote
2 answers

How do the double colons `::` work under the hood in case we use them for method call?

This is a theoretical question. I know that I can call a method by using ::. It is rarely used, but possible. For example: user = User.new user::name The question is: How it is working under the hood? What are the implementation details allowing us…
womanonrails
  • 380
  • 3
  • 7
1
vote
2 answers

Why does PHP execute method of "A" if static:: be resolved to "B"

In this example it first searches in B (because static:: resolves to B) and as it does not find it, it now searches in "A" and that is why it succeeds, right? or am I wrong? class A { private function foo() { echo "success!\n"; } …
1
vote
2 answers

Scope resolution operator for returning a nested class type

I know that the scope resolution operator :: is used to identify and disambiguate identifiers used in different scopes. In the example provided here C++ define class member struct and return it in a member function class UserInformation { public: …
csguy
  • 1,354
  • 2
  • 17
  • 37
0
votes
0 answers

Polymorphysm Derived and Base class with :: Operator

The following programm creates a simple base class(Animal) and some derived classes(Frog,Cat). The question occurs when typing Cat:: or Animal:: (See Code and you'llunderstand the question better) #include using namespace std; class…
0
votes
3 answers

Accessing Nested Class Member Function in CPP

In case of nested class, how do I access the "Inner" or "Child" class's member function?. For example, the code, where I created "obj1". Now how do I access the "childPrint()" with"obj1"? example_code: #include using namespace…
yo only
  • 97
  • 1
  • 2
  • 6
0
votes
2 answers

Why the scope resolution operator (::) does not allow virtual function mechanism? which may lead to infinite recursion otherwise

I was reading about Virtual Functions from the book "The C++ Programming Langauge" by Bjarne Stroustrup, and encountered the following code snippet:- class A { //... protected: int someOtherField; //... public: virtual void…
0
votes
0 answers

C++ scope resolution operator before funcions

namespace Test { struct T {}; void F(T t) {} }; int main() { Test::T t; F(t); // Why no `Test::`? } Why there is no need of Test:: before F(t)? What is the rule behind this?
chaosink
  • 1,329
  • 13
  • 27
0
votes
1 answer

How to create specialization template using scope resolution operator in cpp

template class Temp{ static t x; public: Temp(){}; t increment(); ~Temp(){/*body of destructor is important.*/}; }; templatet Temp::x; template t Temp::increment(){ return…
0
votes
1 answer

calling version of is_const<> but for variables instead of types, in one line

Hi I am learning c++ and I read about type traits such as is_const. is_const can be called in one line like, cout << is_const::value << endl; I made my own version of is_const but to test if a variable is const, and it can be used like…
1
2