Early vs. Late Binding. McGraw-Hill The Complete Reference Fourth Edition
광고
광고
458
C + + : T h e C o m p l e t e R e f e r e n c e
};
} void compute() { val2 = val1 / 3.28;
An important use of abstract classes and virtual functions is in class libraries. You can create a generic, extensible class library that will be used by other programmers.
Another programmer will inherit your general class, which defines the interface and all elements common to all classes derived from it, and will add those functions specific to the derived class. By creating class libraries, you are able to create and control the interface of a general class while still letting other programmers adapt it to their specific needs.
One final point: The base class convert is an example of an abstract class. The virtual function compute( ) is not defined within convert because no meaningful definition can be provided. The class convert simply does not contain sufficient information for compute( ) to be defined. It is only when convert is inherited by a derived class that a complete type is created.
Early vs. Late Binding
Before concluding this chapter on virtual functions and run-time polymorphism, there are two terms that need to be defined because they are used frequently in discussions of C++ and object-oriented programming: early binding and late binding.
Early binding refers to events that occur at compile time. In essence, early binding occurs when all information needed to call a function is known at compile time. (Put differently, early binding means that an object and a function call are bound during compilation.) Examples of early binding include normal function calls (including standard library functions), overloaded function calls, and overloaded operators. The main advantage to early binding is efficiency. Because all information necessary to call a function is determined at compile time, these types of function calls are very fast.
The opposite of early binding is late binding. As it relates to C++, late binding refers to function calls that are not resolved until run time. Virtual functions are used to achieve late binding. As you know, when access is via a base pointer or reference, the virtual function actually called is determined by the type of object pointed to by the pointer. Because in most cases this cannot be determined at compile time, the object and the function are not linked until run time. The main advantage to late binding is flexibility. Unlike early binding, late binding allows you to create programs that can respond to events occurring while the program executes without having to create a large amount of "contingency code." Keep in mind that because a function call is not resolved until run time, late binding can make for somewhat slower execution times.
Chapter 18
Templates
Copyright © 2003 by The McGraw-Hill Companies. Click here for terms of use.
459
다운로드
광고