原文地址: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. ZigZag-LeetCode

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  2. 安装laravel

    # 安装laravel 安装composer #安装 curl -sS https://getcomposer.org/installer | php #添加到PATH sudo mv compose ...

  3. 用phpMyAdmin修改mysql数据库密码

    1初始数据库密码为空. 2第一步,点击phpMyAdmin里的用户选项. 3选择root localhost用户名,点击编辑权限. 4此时会出来修改权限的页面,里面可以设置的选项还是比较多的,暂时不管 ...

  4. php 序列化储存和转化 json_encode() json_decode($q,true)

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 例如:当需要数据库只有一个 ...

  5. mac qq截图功能失效后,如何重启截图功能?

    在finder中打开应用程序目录,找到QQ,右键单击QQ,选择显示包内容,此时会打开一个文件夹. 进入以下路径Library/LoginItems然后双击ScreenCapture这个进程,截图功能即 ...

  6. 基于cygwin构建u-boot(三)make错误忽视

    接上文,修改gcc 的-std标准后,.depend文件处理仍然出现了错误: 五.错误:make中命令报错(sed找不到需要的文件) 错误告警如下: make -C examples/api all ...

  7. MyEclipse8.5自动生成注册码

    package com; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRe ...

  8. hdu 1111 Secret Code

    http://acm.hdu.edu.cn/showproblem.php?pid=1111 复数除法: #include <cstdio> #include <cstring> ...

  9. hdu 1245 Saving James Bond

    http://acm.hdu.edu.cn/showproblem.php?pid=1245 #include <cstdio> #include <cstring> #inc ...

  10. Android CursorAdapter

    CursorAdapter 继承于BaseAdapter是个虚类,它为cursor和ListView提供了连接的桥梁.            public abstract class     Cur ...