Channels ▼


Simulating Polymorphic Operators in C++

Source Code Accompanies This Article. Download It Now.

How Do They Compare?

My primary motivation for exploring alternative versions of polymorphic insertion functions was to create a version that uses less memory than the virtual method version while obtaining comparable performance.

To compare the three versions of code, I experimented with six test cases; see Table 1. The first two test cases use objects and not pointers, so they are nonpolymorphic.

<b>Test 1</b>                  
Employee employee1;   
cout << employee1;

<b>Test 2</b>
Manager m1;
cout << manager1;

<b>Test 3</b>
Employee* pEmp = new Employee;
cout << *pEmp;

<b>Test 4</b>
Manager* pManager1 = new Manager;
cout << *pManager1;

<b>Test 5</b>
Employee* pEmp = new Manager;
cout << *pEmp;

<b>Test 6</b>
Employee* pEmp = new Salesperson;
cout << *pEmp;

Table 1: Test cases.

The first test calls the Employee's insertion function using an Employee object. Likewise, the second test calls the Manager's function using a Manager object. The next two test cases use pointers where the type of the pointer matches the type of the object that is being pointed to by the pointer. The third uses an Employee pointer that is pointing to an Employee object. The fourth test uses a Manager pointer that is pointing to a Manager object. The last two test cases assess the polymorphic behavior of the functions. They both use an Employee pointer that points to a Manager and Salesperson object, respectively.

Figure 2 shows the test timing data for each version. The time unit is microseconds. These numbers are the average of several runs using Visual C++ Version 7.

[Click image to view at full size]

Figure 2: Test timing data for each version.

I expected version 2 to be slower because it uses runtime type information (RTTI), but was surprised that it was so much slower. Keep in mind, version 2 is using just as much memory as version 1.

As I hoped, versions 1 and 3 are comparable in performance. In fact, version 3 is a little faster for the first and third test cases. Both of these test cases deal with parent objects. However, version 3 is a little slower for the test cases dealing with child objects.

Version 1 requires a class to have a vtable, plus every object must contain a pointer to the vtable. Version 3 requires each object to have a type identifier, but this needs less memory than the pointer for a vtable. So, version 3 uses less memory per object and per class.


In this article I presented three different techniques for polymorphic insertion functions. The third version is a viable option for situations with a small class hierarchy where more parent objects are used than children. In these examples, I assumed that there would be many more Employee objects than Manager and Salesperson objects.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.