+ 6

How is a scope resolution operator '::' used specifically?

Scope resolution operator in classes

26th Feb 2019, 9:22 PM
david chongo
david chongo - avatar
1 Antwort
+ 7
When you are defining a class method outside the class, you have to resolve the scope so that the compiler knows the method of which class you are defining, e.g. class A { public: void test(); // method declaration }; void A::test() { std::cout << "Hello, David Chongo!"; } For whatever reason, you might want to create a non-const static variable in your class: class B { public: static int c; /* static int c = 39; */ // error! }; int B::c = 39; // valid You may have also noticed that the scope resolution operator is also used to resolve namespaces, such as the standard namespace. std::cout std::cin std::string Just some trivia: You can also define your own namespaces, and use scope resolution operator in a similar fashion. namespace Fermi { int test = 39; } std::cout << Fermi::test;
27th Feb 2019, 12:16 AM
Fermi
Fermi - avatar