Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if a derived class overrides a method from a base class

Tags:

c++

virtual

rtti

class B {
virtual int foo();
};

class D : public B {
virtual int foo() { cout<<"D\n"; }
};

int B::foo()
{
   /* how do i tell if this->foo() is overridden by a subclass, or if it will */
   /* simply recurse into B::foo()? */
   this->foo();
}

main()
{
D d;
d.B::foo();
}
like image 384
deltamind106 Avatar asked Dec 29 '10 17:12

deltamind106


People also ask

Why would override a method of a base class?

The main advantage of method overriding is that the class can give its own specific implementation to a inherited method without even modifying the parent class code.

Which method of the base class derived class Cannot override?

You cannot override a non-virtual or static method.

How the base class method overrides derived class methods?

An override method is a new implementation of a member that is inherited from a base class. The overridden base method must be virtual, abstract, or override. Here the base class is inherited in the derived class and the method gfg() which has the same signature in both the classes, is overridden.

Do derived classes override inherited methods?

A derived class has the ability to redefine, or override, an inherited method, replacing the inherited method by one that is specifically designed for the derived class. The derived class may want to inherit many of the base class's methods because these methods are suited to the behavior of the derived class.


1 Answers

Answer: you can't.

I'd expand if there was anything to expand upon.

like image 195
Edward Strange Avatar answered Oct 21 '22 17:10

Edward Strange