構造函數調用成員函數&成員函數是虛函數

下列輸出結果是什麼?

include<iostream>
using namespace std;

class A
{
        public:
                A()
                {
                        f();
                }
                virtual void  f()
                {
                        cout<<"a"<<endl;
                }
};

class B : public A
{
        public:
                B()
                {
                        f();
                }
                void f()
                {
                        cout<<"b"<<endl;
                }
};

int main(int argc,char *argv[])
{
        A *a = new B();
        B *b = new B();
        B c;

        int *aa=new int;
        int *bb=new int();
        int *cc=new int[0];
}

g++編譯後運行結果如下:

a
b
a
b
a
b


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章