-2

I am very new to C++ and found the following Code example.

Can the "::" operator be used in this way and why or should it only be used for declared namespaces?

class Test {

    void dosmthng();

};


void Test::dosmthng() {}
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
Teyka
  • 11
  • 3
    If you are learning, I recommend picking a book for this [curated book list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). The code you found is a pretty standard example of C++. So if something about it is unclear, you learning resources may be worth upgrading. – StoryTeller - Unslander Monica Jul 01 '21 at 09:00

2 Answers2

1

The :: operator is called the Scope Resolution Operator. Names declared inside the class are related to the class scope.Searching for names for class scope is subject to some rules.

In what cases is a name searched in class scope ?

If the name is used within a global function, the name must be used in one of 3 cases for the name to be searched in class scope.

  1. If the name is used to the right of the point operator. (.)
  2. If the name is used to the right of the arrow operator. (->)
  3. If the name is used as the right operand of the scope resolution operator. (::)

The 3 important processes and sequences related to name searching are as follows.

  1. Name Lookup => name is searched first.

  2. Context Control=> Other rules of the language . For example, mistakes like using Rvalue where Lvalue is needed.

  3. Access Control => A separate category of controls in Object Oriented programming languages. It is done at compile time.

The left operand of the scope resolution operator must be a name in C++.This name can be a namespace name, it can be a class name.Right operand if class name it will be searched in the scope of that class. To summarize, it is one of the important operators of the language used for name lookup.

east1000
  • 1,240
  • 1
  • 10
  • 30
0

The operator :: is commonly used for both of mentioned cases

slsy
  • 1,257
  • 8
  • 21