码迷,mamicode.com
首页 > 编程语言 > 详细

Is virtual function table really necessary for C++

时间:2014-11-20 11:47:07      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   sp   java   

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. 

Is virtual function table really necessary for C++

标签:des   style   blog   http   io   ar   color   sp   java   

原文地址:http://www.cnblogs.com/linghuaichong/p/4108849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!