注册 登录
编程论坛 C++教室

一个虚函数的问题

m1c2f4843 发布于 2012-04-13 21:56, 276 次点击
程序代码:
#include <iostream>

using namespace std;

class animal
{
    int a;
public:
    virtual void eat()
    {
        cout << "animal eat" << endl;
    }
    virtual void play()
    {
        cout << "animal play" << endl;
    }
};

class dog : public animal
{
    int b;
public:
    virtual void eat()
    {
        cout << "dog eat" << endl;
    }
    virtual void play()
    {
        cout << "dog play" << endl;
    }
    virtual void run()
    {
        cout << "dog run"<< endl;
    }
};


int main()
{
    animal a1;
    dog d1;


    return 0;
}


为什么在对象a1中只有一个animal类的vptr
而没有他自己的vptr,不是在dog类中定义了一个virtual void run();吗 不是应该有一个指向virtual void run();的虚表吗
用的是vc6.0
只有本站会员才能查看附件,请 登录
2 回复
#2
寒风中的细雨2012-04-14 11:33
发重了

当dog不继承别的类时  你可以试试看


子类中可以不加virtual 关键字


[ 本帖最后由 寒风中的细雨 于 2012-4-14 12:21 编辑 ]
#3
寒风中的细雨2012-04-14 11:33
虚函数   是看最顶层的基类的
1