Is virtual function table really necessary for C++

OOP polymorphism 

  In OOP languages, a base-class object pointer can do function call according to the actual type of the object. Let‘s see an example in Java.

public class Animal{
    public void spark(){
        System.out.println("spark");
    }
}
public class Dog extends Animal{
    public void spark(){
        System.out.println("WangWang");
    }
}
public class Cat extends Animal{
    public void spark(){
        System.out.println("MiaoMiao");
    }
}

public class Main{
    public static void main(String[] args){
        Animal[] animals=new Animal[3];
        animals[0]=new Dog();
        animals[1]=new Cat();
        animals[2]=new Animal();
        for(Animal it:animals){
            it.spark()
        }
    }
}

it will output

WangWang
MiaoMiao
spart

That‘s "polymorphism".

C++virtual function

  Above codes is very natual for Java programmers. However in C++, you can‘t get such "polymorphism" without the "virtual" keyword decorating the functions. Without "virtual", C++ will output

spark
spark
spark

  Take a look at  <the virtual table> if you don‘t yet know about virtual table. This article explains why "virtual" came out, and how virtual function is supported by virtual table.

Why virtual table

  Why does C++ use virtual table?

  =>Because C++ compiler does not know the actual function address

     --->Why?

  =>Because C++ compiler does not know the exact type(Cat? Dog? Animal?) of the oject the pointer "panimal" points to

         ---Why? Is that any way compiler can figure out the object type?

  =>Yes! Using "object type tracking"!

object type tracking

Let‘s consider the sources where an object pointer gets its value. 2 sources indeed.

1. another pointer
2. address of class instance

Where does "another pointer" get its value? Eventually, there‘s a pointer that gets its value from "class instance".

So, via tracking the assignment thread backwards to the original source object

  => the compiler is able to figure out the exact type of a pointer.

  =>the compiler knows the address of the exact function being called

  =>no virtual table is needed.

Object type tracking saves both virtual table memery and virtual table pointer of each class instances.

Where does object type tracking not work

Library Linking.

If a library function returns a base-class pointer, there‘s no way for the compiler to track back to the original source object.

时间: 2024-09-30 01:51:55

Is virtual function table really necessary for C++的相关文章

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

Off by One 根据 Halvar Flake 在"Third Generation Exploitation"中的描述,漏洞利用技术依攻击难度从小到大分为三类: 1. 基础的栈溢出利用,可以利用返回地址轻松劫持进程,植入 shellcode,如对 strcpy.strcat 等函数的攻击. 2. 高级栈溢出利用.栈中有限制因素,溢出数据只能淹没部分 EBP,但无法淹没返回地址,不能获得 EIP 控制权.经典例子是对 strnpy 函数误用时产生的 off by one 漏洞.

多重继承下 Virtual Function 的语意

在多重继承中支持 virtual function, 其复杂度围绕在第二个及后继的 base classes 上, 以及必须在执行期调整 this 指针这一点, 以以下的 class 体系为例: class Base1 { public: Base1(); virtual ~Base1(); virtual void SpeakClearly(); virtual Base1* Clone() const; protected: float data_base1; }; class Base2

Template 和 virtual function

Template和virtual function是两种不同类型的多态. Template的多态是在编译期决定的,而virtual function的多态是在运行时决定的. 从应用形式上看,Template是发散式的,让相同的实现代码应用于不同的场合:virtual function是收敛式的,让不同的代码用于相通的场合. 从思维方式上看,Template是泛型式编程风格,看重算法的普适性:virtual function是对象式编程风格,看重的是接口和实现的分离度.

effective c++ 条款9 do not call virtual function in constructor or deconstructor

在构造函数中不要调用virtual函数,调用了也不会有预期的效果. 举个例子 class Transaction { public: Transaction() { log(); } virtual void log() =0; } class BusinessTransaction: public Transaction { public: virtual void log() { ;//log something here } } BusinessTransaction b_trx; b_t

[C++] Pure Virtual Function and Abstract Class

Pure Virtual Function Abstract Class

C++ 实用泛型编程之 虚拟函数(C++ virtual function)杂谈

一 C++虚拟函数(C++ virtual function)杂谈 我们在编程的时候,经常会遇到这样的情况,假设有两个对象,你要在函数中分别调用它们的OnDraw方法,我们以前的做法一般是这样的. void f(int iType) { switch(iType) { case 1: //CCircle OnDraw break; case 2: //CRectangle OnDraw break; } } 这种方法当然能解决我们的问题,但是如果有新的类型要增加,它就必须要往下加代码才行了,这样

OD: Windows Security Techniques &amp; GS Bypassing via C++ Virtual Function

Windows 安全机制 漏洞的万源之本在于冯诺依曼设计的计算机模型没有将代码和数据进行区分——病毒.加壳脱壳.shellcode.跨站脚本攻击.SQL注入等都是因为计算机把数据和代码混淆这一天然缺陷而造成的. Windows XP SP2 之前的系统致力于系统稳定性,忽略安全性:之后的 Windows 系统系统加入了独特的安全性设计: 1. GS 编译技术:函数返回地址之前加入了 Security Cookie,返回之前首先检测 cookie 是否正确,栈溢出难度增加. 2. 增加了对 S.E

C# abstract function VS virtual function?

An abstract function has to be overridden while a virtual function may be overridden. Virtual functions can have a default /generic implementation in the base class. An abstract function can have no functionality. You're basically saying, any child c

pure virtual function call

2015-04-08 10:58:19 基类中定义了纯虚函数,派生类中将其实现. 如果在基类的构造函数或者析构函数中调用了改纯虚函数, 则会出现R6205 Error: pure virtual function call 对象在构造时,会先调用基类构造函数,但此时派生类对象还未构造成功, 因此调用的纯虚函数的虚表指针指向基类的虚表,而基类的纯虚函数没有定义. 如果是在基类的虚构函数中调用,此时的派生类已经被销毁,也会出现这种情况. 1 #include <stdio.h> 2 3 class