C++中奇怪的虚函数

来源:互联网 发布:假装情侣聊天软件 编辑:程序博客网 时间:2024/06/05 14:41
#include<iostream>#include<string>using namespace std;class A {         public:                  virtual string foo(){return "A::foo";}};class B : public A {         public:                  virtual string foo(){return "B::foo";}};int main () {        B* b = new B();        // In my opinion the following should not be allowed        cout << b->A::foo() << endl;  // Will print "A::foo"        cout << b->foo() << endl;  // Will print "B::foo"        A* a = new B();        cout << a->A::foo() << endl;  // Will print "A::foo"        cout << a->foo() << endl;  // Will print "B::foo"}

这个不是有违C++的继承观念吗?

原创粉丝点击