C++ Overloading, overriding and hiding, oh my!

Continuing where I left off previously, I’ve been going through chapter 9, Inheritance and Polymorphism from Ivor Horton’s Beginning Visual C++ 2008 and consulting various other books a little more often than I had been up until now. One of those books was C++ Programming: Visual QuickStart Guide, by Larry Ullman and Andreas Signer.

Edit: There is a slightly better (cleaned up) version here.

I was reading chapter 8, Class Inheritance where I ran afoul of this warning:

Pay attention when overriding methods. If you don’t use the exactly same parameters and return values, you’ll end up with an overloaded method, and not an overridden one. Such mistakes are very hard to debug

I tinkered with the code and tried to figure it out, but I kept coming back to:

  • A derived class function with a different signature than a base class function will result in the base class function of the same name being hidden.
  • A derived class function with the same name and signature as the base class will override the base class function.

While I waited for Larry or another forum goer to find my question where I asked for clarification, I went back to chapter 9, Inheritance and Polymorphism from Ivor Horton’s Beginning Visual C++ 2008 as I mentioned previously. I had left off with the beginning of the chapter, so I could continue from there and eventually catch up in that book where I had run into comprehension issues with the others.

After a week or two, I finally arrived at this statement on page 539:

The class also contains the function ShowVolume(), which displays the volume of objects of derived classes. Because this is declared as virtual, it can be replaced in a derived class, but if it isn’t, the base class version that you see here is called.

Then my brain slipped out of gear again and I’m left with the self-assigned goal to properly define:

  • Overriding
  • Overloading
  • Hiding

Because in the end, I think that the various books, authors (and maybe myself?) are saying the same things, but with different terminology.

Overriding

Ivor Horton said “replaced”, and not overriding, but I am thinking he was referring to the same thing:

Because this is declared as virtual, it can be replaced in a derived class, but if it isn’t, the base class version that you see here (in the base class) is called.

In Thinking in C++, 2e, Volume 1 (pages 632:646), Bruce Eckel had something similar to say, but he did specifically say overriding:

The redefinition of a virtual function in a derived class is usually called overriding

Follow that up with page 130 from C++ Programming in Easy Steps:

A method can be declared in a derived class to override a matching method in the base class – if both method declarations have the same name, arguments and return type.

The C++ FAQ Lite entry, “Should a derived class redefine (“override”) a member function that is non-virtual in a base class?”, recommends against redefining (overriding) a member function that is non-virtual in a base class.

Hiding

Page 130 from C++ Programming in Easy Steps also mentions:

The technique of overriding base class methods must be used with caution however, to avoid unintentionally hiding overloaded methods – a single overriding method in a derived class will hide all overloaded methods of that name in the base class.

C++ Pocket Reference, 1e (page 93) has this to say:

The parameter list and return type for the member function in the derived class must match those of the member function in the base class. Otherwise, the member function of the derived class hides the member function in the base class, and no polymorphic behavior will occur.

Thinking in C++, 2e, Volume 1 (pages 632:646) mentioned this:

If you override one of the overloaded member functions in the base class, the other overloaded versions become hidden in the derived class.

Overloading

My understanding of overloaded methods/functions is what C++ Pocket Reference sums up so well on page 104:

Overloading allows you to provide more than one definition for a function within the same scope.

If I’m correct, the base class and derived classes are separate scopes, so overloading will not occur.

Bjarne Stroustrup had this to say:

In C++, there is no overloading across scopes – derived class scopes are not an exception to this general rule.

Summary

  • redefining, overriding and replacing a function occurs when you have a derived class function with the same function signature as a base class function.
  • Doing so hides all of the other base-class versions of that function.
  • It is not recommended to do this with non-virtual base class functions.
  • jporter892

    Thanks for the very informative post!

    This page helped me grasp the name hiding concept as well:

    C++ name hiding