原文地址: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. poj2378 树形DP

    C - 树形dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  2. <转> 纸牌屋1-4集分析

    原文:http://blog.sina.com.cn/s/blog_b86c61490102v56t.html 第一季第一集 主人公弗兰克的出场,是以对待一只邻家将死之狗的态度展开的,充分显示了主人公 ...

  3. css3之gradient

    radial-gradient The CSS radial-gradient() function creates an <image> which represents a gradi ...

  4. 使用Japserreport填充报表数据(3)

    E中以PDF文件的格式显示静态的中文字符串,在大多数的情况下,打印的数据来自于一些变量,在JasperReports工具中传递数据并填充到 报表只有两种方式,即使用Parameters参数和JRDat ...

  5. 命运(HDU 2571 简单动态规划)

    命运 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  6. Google Chrome源码剖析【序】

    [序(本人什么都没做,完全转载)] 开源是口好东西,它让这个充斥着大量工业垃圾代码和教材玩具代码的行业,多了一些艺术气息和美的潜质.它使得每个人,无论你来自米国纽约还是中国铁岭,都有机会站在巨人的肩膀 ...

  7. java中连接postgresql基本代码

    try { Class.forName( "org.postgresql.Driver" ).newInstance(); String url = "jdbc:post ...

  8. cf C. Tourist Problem

    http://codeforces.com/contest/340/problem/C #include <cstdio> #include <cstring> #includ ...

  9. 编译型/解释型语言,什么时候用shell

    编译型语言 很多传统的程序设计语言,例如Fortran.Ada.Pascal.C.C++和Java,都是编译型语言.这类语言需要预先将我们写好的源代码(source code)转换成目标代码(obje ...

  10. CF 578A A Problem about Polyline

    题意: There is a polyline going through points (0, 0) – (x, x) – (2x, 0) – (3x, x) – (4x, 0) – ... - ( ...