CSC/ECE 517 Fall 2010/ch5 5c IC: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 87: Line 87:
Dynamic dispatch comes in 2 types: [http://en.wikipedia.org/wiki/Dynamic_dispatch#Single_and_multiple_dispatch single and multiple dispatch].  In single dispatch in the case of method overriding, only a method parameter's static type determines which method is called of the list of potential methods to call.   
Dynamic dispatch comes in 2 types: [http://en.wikipedia.org/wiki/Dynamic_dispatch#Single_and_multiple_dispatch single and multiple dispatch].  In single dispatch in the case of method overriding, only a method parameter's static type determines which method is called of the list of potential methods to call.   


For example, if we modify the above example and change the declaration of ''method1'' to  
For example, if we modify the above example and change the declaration of ''method1'' in class ''A'' to  


<pre>
<pre>
virtual int method1(A a);
</pre>


and change method1 in class B to
<pre>
virtual int method1(B b);
</pre>
</pre>



Revision as of 02:04, 2 November 2010

Dynamic Dispatch

Introduction

Dynamic dispatch is an object-oriented programming concept that refers to the mapping of a method to an object's dynamic runtime type. It is common in many object-oriented languages. Languages such as Java and C++ use single dispatch, while only a few, such as CLOS, use multiple dispatch. Both types of dispatch will be discussed later.

Advantages

  • Flexibility
  • Extensibility
  • Reduces the effort required to change implementation [4]

Disadvantages

  • Lookup overhead
  • Counter to safety and increased compile-time knowledge
  • Obstacle to optimization
  • Hinders compiler in determining exact type of objects

Example in C++

Below are two example classes in C++. Class A contains one private attribute and two public virtual methods, method1 and method2. Class B inherits from A, contains one private attribute and two public methods, method1 (which overrides the method defined in class A) and method3.

class A
{
  int aMember1;
public:
  virtual int method1();
  virtual void method2();
};

class B : public A
{
  int bMember1;
public:
  virtual int method1();
  virtual double method3();
}

The next section of code shows a basic implementation of these methods within these two classes.

class A
{
  
  virtual int method1()
  {
    cout << "method1 in class A" << endl;
  }

  virtual int method2()
  {
    cout << "method2 in class A" << endl;
  }
};

class B : public A
{
  virtual int method1()
  {
    cout << "method1 in class B" << endl;
  }

  virtual int method3()
  {
    cout << "method3 in class B" << endl;
  }
}

Since C++ supports the use of polymorphism, an object can be created with a static type of A but a dynamic type of B.

A ab = new B();

Where this presents a challenge is when a method in a subclass overrides a method in a superclass. Since the object ab is of two types, the question arises: From which class will that method be called? Due to dynamic dispatch, the method from the dynamic runtime type will be called. For example, calling method1 on the object ab will call method1 implemented in class B, the object's dynamic type.

ab.method1();

>> method1 in class B

Single vs Multiple Dispatch

Dynamic dispatch comes in 2 types: single and multiple dispatch. In single dispatch in the case of method overriding, only a method parameter's static type determines which method is called of the list of potential methods to call.

For example, if we modify the above example and change the declaration of method1 in class A to

virtual int method1(A a);

and change method1 in class B to

virtual int method1(B b);

Memory Issues

References

[1] “Dynamic Dispatch in Object-Oriented Languages”, 2004.

[2] M. Muller, “Message Dispatch in Dynamically-Typed Object-Oriented Languages”, Master’s Thesis, University of New Mexico, 1995.

[3] http://en.wikipedia.org/wiki/Dynamic_dispatch

[4] D. Schmidt, "Dynamic Binding C++", http://www.cs.wustl.edu/~schmidt/PDF/C++-dynamic-binding4.pdf, 2006.