[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppScopeOperator
The sayHello static member function is a bit of a trivial example (Click here for a better example). More often you use the scope operator when having a derived class calling functions explicitly from its base class:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppScopeOperator
(C++) Scope operator ::
The operator to get into a scope, being the double colons ::. This scope can be in a namespace or a class.Function namespace
Here is a code snippet showing how to have the same functions in two different namespaces and how to call them:namespace myFirstNamespace { void sayHello() { std::cout << "Hello from myFirstNamespace" << std::endl; } } namespace mySecondNamespace { void sayHello() { std::cout << "Hello from mySecondNamespace" << std::endl; } } int main() { std::cout << "Hello from main" << std::endl; myFirstNamespace::sayHello(); mySecondNamespace::sayHello(); }
- include <iostream>
Class namespace
class MyClass { public: static void sayHello() { std::cout << "Hello world" << std::endl; } }; int main() { MyClass::sayHello(); }
- include <iostream>
The sayHello static member function is a bit of a trivial example (Click here for a better example). More often you use the scope operator when having a derived class calling functions explicitly from its base class:
struct MyBaseClass
{
virtual void sayHello() const
{
std::cout << "Hello MyBaseClass" << std::endl;
}
};
struct MyDerivedClass : public MyBaseClass
{
void sayHello() const
{
std::cout << "Hello MyDerivedClass" << std::endl;
}
void lie() const
{
MyBaseClass::sayHello();
}
};
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
