[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


Calling an Inherited Member Function

(ARM, §10.2) If you want to call an inherited virtual member function, rather than the local override of that function, you can do so in two ways. The first way is the recommended method for referring to member functions defined in a base class, or any other parent class. The second way, while more convenient, is not recommended if you intend to use your source code with other compilers.


The standard way to call inherited member functions

The first way is supported by the ANSI/ISO C++ Standard and simply qualifies the member function with its base class.

Assume you have two classes, MyBaseClass and MySubClass, each implementing a function named MyFunc().

From within a function of MySubClass, you can call the base class version of MyFunc() this way:


MyBaseClass::MyFunc();

However, if you change the class hierarchy, this code may break. Assume you introduce an intermediate class, and your hierarchy is now MyBaseClass, MyMiddleClass, and MySubClass. Each has a version of MyFunc(). The code above still calls the original version of MyFunc() in the MyBaseClass, bypassing the additional behavior you implemented in MyMiddleClass. In all likelihood, this is not what you intend, and this kind of subtlety in the code can lead to unexpected results and bugs that are very hard to locate.


Using inherited keyword to call inherited member functions


NOTE

The inherited keyword is not supported by the ANSI/ISO C++ standard and is only implemented for single inheritance with CodeWarrior C++.

You may call the inherited version of MyFunc() this way:


inherited::MyFunc();

With the inherited keyword, the compiler identifies the base class at compile time. This line of code would call the immediate base class in both cases: where the base class is MyBaseClass, and where the immediate base class is MyMiddleClass.

If your class hierarchy changes at a later date and your subclass inherits from a different base class, the immediate base class is still called, despite the change in hierarchy.

The syntax is the following:


  inherited::func-name(param-list);

The statement calls the func-name in the class's immediate base class. If class has more than one immediate base class (because of multiple inheritance) and the compiler can't decide which func-name to call, the compiler generates an error.

This example creates a Q class that draws its objects by adding behavior to the O class.

Using inherited to call an inherited member function:
class O { virtual void draw(Point); }
class Q : O { void draw(Point); }

void O::draw (Point p)
{
  Rect r = { p.x-5, p.y-5, p.x+5, p.y+5 };
  FrameOval(r);        // Draw an O.
}


void Q::draw (Point p)
{
  inherited::draw(p);   // Perform behavior of base class
  MoveTo(p.x, p.y);     // Perform added behavior
  Line(5, 5);
}

Make sure to insert this directive before using the inherited keyword:


  #pragma def_inherited on

For related information on this pragma see "def_inherited."


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 17, 2000