Calling identically named methods in base classes

The base class Ahas a subclass B, but Bhas a subclass C. Aimplements a virtual method doStuff(), Bno, as well C. From C, I want to call the Aimplementation doStuff()(I am doing this from the Cimplementation doStuff(), but it does not really matter.) I need to call:

A::doStuff();

Or:

B::doStuff();

The first seems more understandable, since it refers to the actual implementation. On the other hand, the second may be more useful if I later decide that I Bshould do doStuff()otherwise A. Which is more standard? Which is more dangerous?

I was a little surprised that I B::doStuff()did not raise any warnings, but I believe that by definition it Bhas an implementation, even if it is from a base class. Can a chain of base classes and implementations be arbitrarily long and complex? For example, if I had Athrough Z, each of the subclasses of the previous one, could I have implementations at any point in the chain and call a method of the class, just go through the "chain" until it finds an implementation?

+5
source share
2 answers

Firstly, regarding the last paragraph, yes, you are absolutely right.

Further, for the main question:

, . , , B A, B::doStuff() . .

. , , C - A, , D (sibling of C) , . :

class A
{
private:
   void doAStuff(); //doesn't have to be here
public:
   virtual void doStuff()
   {
      doAStuff();
   }
   void doAStuffAndStuff();
   {
      doAStuff();
      doStuff();
   }
}

class B : A
{
public:
   virtual void doStuff(); //or not
}

class C : B
{
public:
   virtual void doStuff();
}

. , A* a, A:

  • doStuff() ​​ , doStuff().
  • doStuff() ​​ , , A, doAStuffAndStuff().

. , , .

+2

B::doStuff. , , , , . , base.doStuff - , . ( ) BaseType BaseType::doStuff.

, B:: doStuff() , , B ,

. .

, , . , - , , , .

0

All Articles