Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17378
  • 博文数量: 22
  • 博客积分: 920
  • 博客等级: 准尉
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-27 15:19
文章分类

全部博文(22)

文章存档

2010年(22)

我的朋友
最近访客

分类: C/C++

2010-03-27 16:07:41

Remeber first: virtual table's construction isn't finished before the class's constructor, so the polymorphism is unavailable during the construction of the class.

e.g

 

class Base
{
public:
    Base() { sayHello(); }
    virtual void sayHello() { cout << "Base says hello\n"; }
};

class Derived : public Base
{
public:
    void sayHello() { cout << "Derived says hello\n"; }
};

int main(int argc, char *argv[])
{
    Derived d;
    return 0;
}

输出结果:
   Base says hello


阅读(187) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~