(转) Virtual function
原文地址:http://en.wikipedia.org/wiki/Virtual_function
In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is an important part of the polymorphism portion of object-oriented programming (OOP).
- 1 Purpose
- 2 Example
- 3 Abstract classes and pure virtual functions
- 4 Behavior during construction and destruction
- 5 Virtual destructors
- 6 See also
- 7 References
Purpose[edit]
Further information: Dynamic dispatchThe concept of the virtual function solves the following problem:
In object-oriented programming when a derived class inherits from a base class, an object of the derived class may be referred to via a pointer or reference of either the base class type or the derived class type. If there are base class methods overridden by the derived class, the method actually called by such a reference or pointer can be bound either 'early' (by the compiler), according to the declared type of the pointer or reference, or 'late' (i.e. by the runtime system of the language), according to the actual type of the object referred to.
Virtual functions are resolved 'late'. If the function in question is 'virtual' in the base class, the most-derived class's implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. If it is not 'virtual', the method is resolved 'early' and the function called is selected according to the declared type of the pointer or reference.
Virtual functions allow a program to call methods that don't necessarily even exist at the moment the code is compiled.
In C++, virtual methods are declared by prepending the virtual keyword to the function's declaration in the base class. This modifier is inherited by all implementations of that method in derived classes, meaning that they can continue to over-ride each other and be late-bound.
// inner block scopes
#include <iostream>
usingnamespacestd;
int main () {
;
;
{
int x; // ok, inner scope.
; // sets value to inner x
; // sets value to (outer) y
cout << "inner block:\n";
cout << "x: " << x << '\n';
cout << "y: " << y << '\n';
}
cout << "outer block:\n";
cout << "x: " << x << '\n';
cout << "y: " << y << '\n';
;
}
(转) Virtual function的更多相关文章
- pure virtual function call
2015-04-08 10:58:19 基类中定义了纯虚函数,派生类中将其实现. 如果在基类的构造函数或者析构函数中调用了改纯虚函数, 则会出现R6205 Error: pure virtual fu ...
- C# abstract function VS virtual function?
An abstract function has to be overridden while a virtual function may be overridden. Virtual functi ...
- Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法
我的Mindjet MindManager 2012 Pro也就是MindManager10 在应用模板之后总会显示 Microsoft Visual C++ Runtime Library Runt ...
- OD: Windows Security Techniques & GS Bypassing via C++ Virtual Function
Windows 安全机制 漏洞的万源之本在于冯诺依曼设计的计算机模型没有将代码和数据进行区分——病毒.加壳脱壳.shellcode.跨站脚本攻击.SQL注入等都是因为计算机把数据和代码混淆这一天然缺陷 ...
- OD: Memory Attach Technology - Off by One, Virtual Function in C++ & Heap Spray
Off by One 根据 Halvar Flake 在“Third Generation Exploitation”中的描述,漏洞利用技术依攻击难度从小到大分为三类: . 基础的栈溢出利用,可以利用 ...
- why pure virtual function has definition 为什么可以在基类中实现纯虚函数
看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...
- [c++] polymorphism without virtual function
polymorphism without virtual function
- [C++] Pure Virtual Function and Abstract Class
Pure Virtual Function Abstract Class
- 纯虚函数(pure virtual function )和抽象类(abstract base class)
函数体=0的虚函数称为“纯虚函数”.包含纯虚函数的类称为“抽象类” #include <string> class Animal // This Animal is an abstract ...
随机推荐
- attachEvent和addEventListener
attachEvent和addEventListener在前端开发过程中经常性的使用,他们都可以用来绑定脚本事件,取代在html中写obj.onclick=method. 相同点: 它们都是dom对象 ...
- javascript区分电脑与手机登陆
<script language="javascript"> function checkMobile() { var pda_user_agent_list = ne ...
- OSharp 学习(一)
OSharp3.0 由郭明峰组织开发,已经相对比较完整,从底层代码开始阅读难度比较大. 故采用自上而下的方式,使用到的时候再完善相关功能.希望通过此教程能够找到志同道合的人. 注:请勿抄袭 请参看OS ...
- MyFirstStruts2
package com.sdlc.action; public class HelloWorldAction { private String msg; public void setMessage( ...
- JVM virtual memory
This has been a long-standing complaint with Java, but it's largely meaningless, and usually based o ...
- HashMap Java Doc
原文 public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneab ...
- 【POJ1581】A Contesting Decision(简单模拟)
没有什么弯路,直接模拟即可.水题. #include <iostream> #include <cstring> #include <cstdlib> #inclu ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅵ
· 学后心得体会与部分习题实现 心得体会: 曾经只是了解了优先队列的基本性质,并会调用C++ STL库中的priority_queue以及 java.util.PriorityQueue<E&g ...
- 苹果拒绝App内部使用版本检测功能
10.6 - Apple and our customers place a high value on simple, refined, creative, well thought through ...
- Vlc for Android 全面阐述
简单介绍 Vlc for android是一款开源安卓播放器.具备播放多媒体文件.光盘.设备以及网络流媒体协议等功能,支持ARMv7 CPU或一个x86 CPU的设备,全部播放控制特性都已经开发完整. ...