Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
July 02, 2007
Simulating Polymorphic Operators in C++

Without using extra virtual methods

(Page 1 of 5)
Michael Dowell
Michael presents three different techniques for making operators polymorphic.
Michael is an Assistant Professor of Computer Science at Augusta State University. He can be contacted at mdowell@aug.edu.


In C++, polymorphism lets a variable refer to objects of different data types. The only catch is that the different data types must be members of the same inheritance hierarchy and they must be lower in the hierarchy than the variable's data type. The cool part is this ability lets the same instruction behave differently, based on the actual object's data type instead of the variable's data type.

Consider the hierarchy of the UML class diagram in Figure 1. Here, I have derived the Manager and Salesperson classes from the Employee class. This lets me use an Employee pointer to point to an Employee, Manager, or Salesperson object.

[Click image to view at full size]

Figure 1: A class hierarchy.

The following code is an attempt at polymorphism:

Employee* pEmp = new Manager;
cout << *pEmp << endl;

However, this attempt fails since the code does not invoke the Manager's insertion operator (<<) as I intended—it invokes the Employee's insertion operator. This happens because the indirection operation (*) is performed first, so *pEmp returns the data type of the pointer. In this case, because pEmp is an Employee pointer, the *pEmp operation returns the Employee data type in spite of pointing to a Manager object. After *pEmp is done, the function call is matched using the Employee data type. Consequently, the Employee's insertion function is matched and not the Manager's function as intended.

In C++, polymorphism normally requires virtual methods. Because only methods can be inherited, many programmers think the insertion (<<) and extraction (>>) operators cannot display polymorphic behavior because these operators are implemented as nonmember friend functions. However, there are several ways of making these operators polymorphic. In this article, I compare and contrast three different techniques.

1 Simulating Polymorphic Operators in C++ | 2 Version 1: Auxiliary Methods | 3 Version 2: The dynamic_cast Operator | 4 Version 3: An Enumerated Type | 5 How Do They Compare? Next Page
TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:



    MICROSITES
    FEATURED TOPIC

    ADDITIONAL TOPICS

    INFO-LINK



     



    Related Sites: DotNetJunkies, SD Expo, SqlJunkies