激情与梦想

北辰之神,苍青之君,广兮长空,以翱以翔
    <<  奇虎董事长周鸿祎:产品秘笈–小版本成就大产品--[转][超精彩] | 首 页 | 都是染色体复制惹的祸[转]  >>
  • 2009-10-16

    polymiorphism of C++ - [C++]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://zongtongyi.blogbus.com/logs/48515553.html

    There days, I joined a written examination, and I found a intereting topic about C++'s polymiorphism, so I wrote some extend codes after I came back, here it is:

    #include <iostream>
    using namespace std;

    class Base
    {
    public:

        int b;
        virtual
    void f(void){cout << "Base::f(int) " << endl;}
    };

    class Derive:public Base
    {
    public:

        int d;
        virtual void f(void){cout << "Derive::f(int) " << endl;}
    };

    int main()
    {
        Base b;
        Derive d;
        Base *pb = &d;
        Base &rb = d;

        b.f();
        d.f();
        pb->f();
        rb.f();

        cout <<sizeof(int)<<endl
            <<sizeof(Base) << endl
            <<sizeof(*pb) << endl
            <<sizeof(Derive)<<endl;

        return true;
    }

    Compiled on Ubuntu 8.04 、 g++ .
    the f(void) with or without "virtual" will change the output

    1、without virtual, the output will be :

    Base::f(int)
    Derive::f(int)
    Base::f(int)
    Base::f(int)
    4
    4
    4
    8

    because the Derive overlap Base's f(void), no matter pointer or refer

    sizeof(Base) is "int b" 's size, it's 4; sizeof(*pb) is the same to sizeof(Base); sizeof(Derive) is "int b" plus "int d", so it's 8.


    2、with virtual, the output will be :

    Base::f(int)
    Derive::f(int)
    Derive::f(int)
    Derive::f(int)
    4
    8
    8
    12

    because the Derive overload Base's f(void), no matter pointer or refer

    sizeof(Base) is "int b" plus "virtual f(void)" 's pointer, pointer's size is 4; sizeof(*pb) is the same to sizeof(Base); sizeof(Derive) is "int b" plus "int d" plus "virtual f(void)" 's pointer, just one pointer for "virtual f(void)".


    历史上的今天:

    都是染色体复制惹的祸[转] 2009-10-16

    随机文章:

    让人大彻大悟的孔子临终遗言[转] 2009-05-25
    〔zt〕c/c++编程基础篇之浅析堆&栈〔精〕 2007-04-20
    Linux 用户态与内核态的交互[转]〔保留〕 2007-02-01
    http隧道[转] 2006-07-22
    [转载]编程修养-------精品(一) 2006-04-30

    收藏到:Del.icio.us




    Tag:
    引用地址:
    zongtongyi 发表于12:34:50 | 编辑 | 继续话题 | 转发 | 分享 0

搜索

总统的最新教义

  • KDE i18n()
  • 都是染色体复制惹的祸[转]
  • polymiorphism of C++
  • 奇虎董事长周鸿祎:产品秘笈–小版本成就大产品--[转][超精彩]
  • 在路上[转]--《The Pursuit of Happyness》影评
  • The journey of a packet through the linux 2.4 network stack [zt]
  • 【转】当你的屁股在男人的腿上晃来晃去-【窃听风云】影评
  • 空,孤
  • Dijkstra
  • 驱动[转]
全部日志>>

朋友们说

  • lll:你还在北京?
  • 11:4.15--5.18,一个多月了。。。该更新点了吧...
  • zt:文章内容和文章名字都是转载的,解释权属于原作者。 ...
  • 另论:总统阁下,您可以撞墙了。需要帮忙么...
  • 大君:失败只有一种, 那就是半途而废。 赫赫,加油! 呵呵...
  • ranger:你好啊,我猜,你应该是在Mozilla上说过修改fire...
  • 蝶恋花:是 这就是小说中描述恶人的脸时都是扭曲的原因 够...
  • 蝶恋花:“此情可待成追忆,只是当时已惘然。”李商隐的一首词《无题...
  • drrr:szBuf[i] = 0;才是关键 是因为C/C++字...
  • :(:当我没看见。鄙视你。...
  • RSS 什么是RSS?
    用IM提醒我内容更新
    订阅到QQ邮箱
    订阅到鲜果阅读器
    订阅到Google阅读器
    订阅到抓虾阅读器
  • 《城客》第四期:创意之城
    博客大巴
    博客大巴使用指南
    博客大巴模板中心
    免费注册博客大巴
    一键博客搬家工具
    中文互动杂志城客
Copyright © 2002-2009 BlogBus.com, All Rights Reserved. 博客大巴 版权所有
博客大巴模板设计:孤寂行星 | 作者:iZ