3.19 Scope Resolution Operator


We have seen the use of the scope resolution operator (::) to explicitly specify the class identity of a method or state. We can also use the scope resolution operator to access global variables, for example:

  int x;
  
  class A {
    int x;
    virtual void someMethod()
    {
      x++;    //increments the A::x
      ::x++;  //increments the global x
    }
  };
  

What about using overloading and hiding?.. Example:

  class A{
    public:
      virtual int f(int);
  };
  
  class B: public A {
    public:
      virtual int f(char *);
  };

  int main()
  {
    B b;
    b.f(2);       //Error - B::f(char *) hides A::f(int)
    b.A::f(2);    //fine
    b.f("test");  //fine
  }



These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
Comments