Is there a way to call a base class method of an object that overstates? (C ++)

I know some languages ​​allow this. Is this possible in C ++?

+2


source to share


3 answers


Yes:



#include <iostream>

class X
{
    public:
        void T()
        {
            std::cout << "1\n";
        }
};

class Y: public X
{
    public:
        void T()
        {
            std::cout << "2\n";
            X::T();             // Call base class.
        }
};


int main()
{
    Y   y;
    y.T();
}

      

+5


source


class A
{
  virtual void foo() {}
};

class B : public A
{
   virtual void foo()
   {
     A::foo();
   }
};

      



+2


source


Yes, just specify the type of the base class.

For example:

#include <iostream>

struct Base
{
    void func() { std::cout << "Base::func\n"; }
};

struct Derived : public Base
{
    void func() { std::cout << "Derived::func\n"; Base::func(); }
};

int main()
{
    Derived d;
    d.func();
}

      

+1


source







All Articles