(转) 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 ...
随机推荐
- 在js传递参数中含加号(+)的处理方式
一般情况下,URL 中的参数应使用 url 编码规则,即把参数字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+).但是对于带有中文的参数来 ...
- eclipse修改字体
修改xml字体: window→Preferences→General→Colors and Fonts→Basic→Text Font
- 库函数 Math
int abs( int num ); double fabs( double arg ); long labs( long num ); 函数返回num的绝对值 #include <mat ...
- js+jquery+html实现在三种不通的情况下,点击图片放大的效果
js+jquery+html实现在三种不通的情况下,点击图片放大的效果. 三种情况分别是:图片的父元素宽高固定; 图片的宽高固定; 图片的父元素宽固定,高度不固定 第一种情况:图片的父元素宽高固定 ...
- Jasper_crosstab_columngroup header position config - (headerPosition="Stretch")
Position of Totals RowThe totalPosition attribute controls the appearance of the row that displays t ...
- 图铭Android平台银行卡号识别系统
随着智能终端(智能手机及平板电脑)及移动通信(3G)的发展,原来运行在PC上的信息系统(如邮件系统.即时通信.网页浏览.协同办公.网络购物.社交网站.博客等)逐渐转移到智能终端设备上.可以预见未来几年 ...
- YII学习笔记-登录后的session的总结
在YII框架的默认的登录后的session数据是id,name,__states这三个数据. 在搭配好YII框架环境后,可以使用admin/admin,来登录系统.如果在protected/views ...
- GetProcessMemoryInfo API取得进程所用的内存
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683219(v=vs.85).aspx 例子: https://msdn.mic ...
- SendMessage参数
http://download.csdn.net/download/wshjldaxiong/4830242
- The end of other
The end of other For language training our Robots want to learn about suffixes. In this task, you ar ...