zoukankan      html  css  js  c++  java
  • c++ 多态问题(在虚函数里调用虚函数)

    最近在看cocos2d-x的源码,非常感激cocos2d作者的开源精神。在看代码的过程中感觉两个方向让我受益,1.把之前从书中看到的c++知识,明白了怎么运用。2.学习作者驾驭代码的巧妙方法。

    看coco2d-x的时候我发现了自己对多态之前不了解的知识,废话少说上代码:

    #include <iostream>
    #include <string>
    using namespace std;
    class parent
    {
    public:
        virtual void printMsg();
        virtual std::string title();
    };
    std::string parent::title()
    {
        return "i am parent";
    }
    void parent::printMsg()
    {
        std::cout<< title() << std::endl;
    }
    
    class child:public parent
    {
    public:
        virtual void printMsg();
        virtual std::string title();
    };
    
    std::string child::title()
    {
        return "i am child";
    }
    void child::printMsg()
    {
        parent::printMsg();
        //std::cout<< title() << std::endl;
    }
    
    int main()
    {
        child *p = new child();
        p->printMsg();
        getchar();
        return 0;
    }

    打印信息为:i am child

  • 相关阅读:
    Go 打印出结构化结构体
    GOPROXY设置
    python判断链表是否有环
    单链表python和go的代码
    mongo索引
    python修改srt字幕的时间轴
    python各个版本的排序
    mac使用python识别图形验证码
    selenium运行js代码笔记
    布隆过滤器
  • 原文地址:https://www.cnblogs.com/onlycxue/p/3287630.html
Copyright © 2011-2022 走看看