没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Dynamic Binding C++
Douglas C. Schmidt
Professor Department of EECS
d.schmidt@vanderbilt.edu Vanderbilt University
www.dre.vanderbilt.edu/
schmidt/ (615) 343-8197
Douglas C. Schmidt OO Programming with C++
Motivation
When designing a system it is often the case that developers:
1. Know what class interfaces they want, without precisely knowing
the most suitable representation
2. Know what algorithms they want, without knowing how particular
operations should be implemented
In both cases, it is often desirable to
defer
certain decisions as long
as possible
– Goal: reduce the effort required to change the implementation
once enough information is available to make an informed
decision
Copyright
c
1997-2006 Vanderbilt University 1
Douglas C. Schmidt OO Programming with C++
Motivation (cont’d)
Therefore, it is useful to have some form of abstract “place-holder”
– Information hiding & data abstraction provide compile-time &
link-time place-holders
i.e.
, changes to representations require recompiling and/or
relinking...
– Dynamic binding provides a
dynamic
place-holder
i.e.
, defer certain decisions until run-time
without
disrupting
existing code structure
Note, dynamic binding is orthogonal to dynamic linking...
Dynamic binding is less powerful than pointers-to-functions, but
more comprehensible & less error-prone
–
i.e.
, since the compiler performs type checking at compile-time
Copyright
c
1997-2006 Vanderbilt University 2
Douglas C. Schmidt OO Programming with C++
Motivation (cont’d)
Dynamic binding allows applications to be written by invoking
general
methods via a base class pointer,
e.g.
,
class Base { public: virtual int vf (void); };
Base *bp = /* pointer to a subclass */;
bp->vf ();
However, at
run-time
this invocation actually invokes more
specialized
methods implemented in a derived class,
e.g.
,
class Derived : public Base
{ public: virtual int vf (void); };
Derived d;
bp = &d;
bp->vf (); // invokes Derived::vf()
In C++, this requires that both the general and specialized methods
are virtual methods
Copyright
c
1997-2006 Vanderbilt University 3
Douglas C. Schmidt OO Programming with C++
Motivation (cont’d)
Dynamic binding facilitates more flexible and extensible software
architectures,
e.g.
,
– Not all design decisions need to be known during the initial stages
of system development
i.e.
, they may be postponed until run-time
– Complete source code is not required to extend the system
i.e.
, only headers & object code
This aids both
flexibility
&
extensibility
– Flexibility = ‘easily recombine existing components into new
configurations’
– Extensibility = “easily add new components”
Copyright
c
1997-2006 Vanderbilt University 4
Douglas C. Schmidt OO Programming with C++
Dynamic vs. Static Binding
Inheritance review
– A pointer to a derived class can always be used as a pointer to a
base class that was inherited
publicly
Caveats:
The inverse is not necessarily valid or safe
Private
base classes have different semantics...
–
e.g.
,
template <typename T>
class Checked_Vector : public Vector<T> { ... };
Checked_Vector<int> cv (20);
Vector<int> *vp = &cv;
int elem = (*vp)[0]; // calls operator[] (int)
– A question arises here as to which version of operator[] is called?
Copyright
c
1997-2006 Vanderbilt University 5
Douglas C. Schmidt OO Programming with C++
Dynamic vs. Static Binding (cont’d)
The answer depends on the type of binding used...
1.
Static Binding
: the compiler uses the type of the pointer to
perform the binding at compile time. Therefore,
Vector::operator[](vp, 0) will be called
2.
Dynamic Binding
: the decision is made at run-time based upon
the type of the actual object. Checked
Vector::operator[]
will be called in this case as (*vp->vptr[1])(vp, 0)
Quick quiz: how must class Vector be changed to switch from static
to dynamic binding?
Copyright
c
1997-2006 Vanderbilt University 6
Douglas C. Schmidt OO Programming with C++
Dynamic vs. Static Binding (cont’d)
When to chose use different bindings
–
Static Binding
Use when you are sure that any subsequent derived classes
will not want to override this operation dynamically (just
redefine/hide)
Use mostly for reuse or to form “concrete data types”
–
Dynamic Binding
Use when the derived classes may be able to provide a different
(
e.g.
, more functional, more efficient) implementation that
should be selected at run-time
Used to build dynamic type hierarchies & to form “abstract data
types”
Copyright
c
1997-2006 Vanderbilt University 7
Douglas C. Schmidt OO Programming with C++
Dynamic vs. Static Binding (cont’d)
Efficiency
vs.
flexibility
are the primary tradeoffs between static &
dynamic binding
Static binding is generally more efficient since
1. It has less time & space overhead
2. It also enables method inlining
Dynamic binding is more flexible since it enables developers to
extend the behavior of a system transparently
– However, dynamically bound objects are difficult to store in
shared memory
Copyright
c
1997-2006 Vanderbilt University 8
Douglas C. Schmidt OO Programming with C++
Dynamic Binding in C++
In C++, dynamic binding is signaled by explicitly adding the keyword
virtual in a method declaration,
e.g.
,
struct Base {
virtual int vf1 (void) { cout << "hello\n"; }
int f1 (void);
};
– Note, virtual methods
must
be class methods,
i.e.
, they cannot be:
Ordinary “stand-alone” functions
class data
Static methods
Other languages (
e.g.
, Eiffel) make dynamic binding the default...
– This is more flexible, but may be less efficient
Copyright
c
1997-2006 Vanderbilt University 9
Douglas C. Schmidt OO Programming with C++
C++ Virtual Methods
Virtual methods have a fixed
interface
, but derived
implementations
can change,
e.g.
,
struct Derived_1 : public Base
{ virtual int vf1 (void) { cout << "world\n"; } };
Supplying virtual keyword is optional when overriding vf1() in
derived classes,
e.g.
,
struct Derived_2 : public Derived_1 {
int vf1 (void) { cout << "hello world\n"; } // Still virtual
int f1 (void); // not virtual
};
You can declare a virtual method in any derived class,
e.g.
,
struct Derived_3 : public Derived_2 {
virtual int vf2 (int); // different from vf1!
virtual int vf1 (int); // Be careful!!!!
};
Copyright
c
1997-2006 Vanderbilt University 10
Douglas C. Schmidt OO Programming with C++
C++ Virtual Methods (cont’d)
Virtual method dispatching uses object’s “dynamic type” to select
the appropriate method that is invoked at run-time
– The selected method will depend on the class of the
object
being
pointed at &
not
on the pointer type
e.g.
,
void foo (Base *bp) { bp->vf1 (); /* virtual */ }
Base b;
Base *bp = &b;
bp->vf1 (); // prints "hello"
Derived_1 d;
bp = &d;
bp->vf1 (); // prints "world"
foo (&b); // prints "hello"
foo (&d); // prints "world"
Copyright
c
1997-2006 Vanderbilt University 11
剩余12页未读,继续阅读
资源评论
fowlersong
- 粉丝: 7
- 资源: 79
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 机器学习和数据挖掘课程设计-米其林餐厅数据挖掘管理系统源码+使用文档说明.zip
- html html html展示我与ai的对化
- 数据结构课程设计-全国交通出行咨询模拟系统C语言实现源码.zip
- cef-binary-109.0.1+gcd5e37a+chromium-109.0.5414.8-windows32
- 基于C语言的全国交通咨询系统模拟源码.zip
- 正点原子HAL库 STM32F4 DMA(学习自用附源码)
- 炫酷代码雨,超级炫酷哦!!!
- 基于物联网MQTT协议的智能停车场管理系统
- POETIZE个人博客系统源码 - 最美博客
- 基于深度学习的行人检测系统源码+项目说明(YoloV3+Tensorflow).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功