小编Bal*_*ran的帖子

为什么使用基类指针来派生类

class base{
    .....
    virtual void function1();
    virtual void function2();
};

class derived::public base{
    int function1();
    int function2();
};

int main()
{
    derived d;
    base *b = &d;
    int k = b->function1() // Why use this instead of the following line?
    int k = d.function1(); // With this, the need for virtual functions is gone, right?

}
Run Code Online (Sandbox Code Playgroud)

我不是CompSci工程师,我想知道这一点.如果我们可以避免基类指针,为什么要使用虚函数?

c++ oop polymorphism virtual-functions base-class

21
推荐指数
1
解决办法
2万
查看次数

标签 统计

base-class ×1

c++ ×1

oop ×1

polymorphism ×1

virtual-functions ×1