原文地址: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 dispatch

    The 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的更多相关文章

  1. pure virtual function call

    2015-04-08 10:58:19 基类中定义了纯虚函数,派生类中将其实现. 如果在基类的构造函数或者析构函数中调用了改纯虚函数, 则会出现R6205 Error: pure virtual fu ...

  2. C# abstract function VS virtual function?

    An abstract function has to be overridden while a virtual function may be overridden. Virtual functi ...

  3. Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法

    我的Mindjet MindManager 2012 Pro也就是MindManager10 在应用模板之后总会显示 Microsoft Visual C++ Runtime Library Runt ...

  4. OD: Windows Security Techniques & GS Bypassing via C++ Virtual Function

    Windows 安全机制 漏洞的万源之本在于冯诺依曼设计的计算机模型没有将代码和数据进行区分——病毒.加壳脱壳.shellcode.跨站脚本攻击.SQL注入等都是因为计算机把数据和代码混淆这一天然缺陷 ...

  5. OD: Memory Attach Technology - Off by One, Virtual Function in C++ & Heap Spray

    Off by One 根据 Halvar Flake 在“Third Generation Exploitation”中的描述,漏洞利用技术依攻击难度从小到大分为三类: . 基础的栈溢出利用,可以利用 ...

  6. why pure virtual function has definition 为什么可以在基类中实现纯虚函数

    看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...

  7. [c++] polymorphism without virtual function

    polymorphism without virtual function

  8. [C++] Pure Virtual Function and Abstract Class

    Pure Virtual Function Abstract Class

  9. 纯虚函数(pure virtual function )和抽象类(abstract base class)

    函数体=0的虚函数称为“纯虚函数”.包含纯虚函数的类称为“抽象类” #include <string> class Animal // This Animal is an abstract ...

随机推荐

  1. poj2709 贪心基础

    D - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bi ...

  2. JavaScript高级程序设计(学习笔记)

    第13章 事件 一.事件 1.1事件冒泡:事件发生时从里面向外传播   如:div>body>html>document 1.2事件捕获:事件发生时从外层向里层传播   如  doc ...

  3. WordPress页面函数功能代码调用大全

    WordPress模板基本文件 style.css 样式表文件index.php 主页文件single.php 日志单页文件page.php 页面文件archvie.php 分类和日期存档页文件sea ...

  4. python - num1 -初识python

    一.了解python python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  5. Tempter of the Bone--hdu1010--zoj2110

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. php生成json和js解析json

    php生成json $value=array("name"=>"dfas");echo json_encode($value); js解析json var ...

  7. mybatis批量update(mysql)

    Mapper文件中的写法 <insert id="batchUpdateTjData"> <foreach collection="list" ...

  8. UVa 1449 - Dominating Patterns (AC自动机)

    题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...

  9. 诺心(LECAKE) | 氪加

    诺心(LECAKE) | 氪加 诺心(LECAKE)

  10. js实现a标签超链接提交form表单的方法

    <a class="regButton"    id="saveRegister" onclick="document.getElementBy ...