zoukankan      html  css  js  c++  java
  • c++虚函数重写的权限问题

    cbase.h:

     #ifndef CBASE_H

    #define CBASE_H
    #include<iostream>
    using std::cout;
    using std::endl;
    class CBase{
    public:
        virtual void show();
    };
    #endif // CBASE_H

    cbase.cpp:
    #include"cbase.h"
    void CBase::show()
    {
        cout<<"CBase"<<endl;
    }

    cderived.h:
    #ifndef CDERIVED_H
    #define CDERIVED_H
    #include"cbase.h"
    class CDerived:public CBase{
    private:
        void show() override;
    };
    #endif // CDERIVED_H

    cderived.cpp:
    #include"cderived.h"
    void CDerived::show()
    {
        cout<<"CDerived"<<endl;
    }
    
    
    main.cpp:
    #include <iostream>
    #include"cderived.h"
    using namespace std;
    
    
    int main()
    {
        CBase* pBase=new CDerived;
        pBase->show();
        dynamic_cast<CDerived*>(pBase)->show();
        delete pBase;
    
    
        CDerived* pDerived=new CDerived;
        pDerived->show();
        static_cast<CBase*>(pDerived)->show();
        delete pDerived;
    return 0
    }
    程序运行会报错:dynamic_cast<CDerived*>(pBase)->show();pDerived->show();
    说是私有的不可调用
    可见编译器只看到了他所看到的,要从编译器的角度看问题。只是把指针是什类型就是什么类型。理解(*(p->vptr)[n])(p,...)


    
    
  • 相关阅读:
    codeforces 363B
    hdu 1075 字典树
    D
    C A Simple Job
    Washing Plates 贪心
    HDU 4143 A Simple Problem 分解因式
    ACdream 1236 Burning Bridges 割边 + 去重边
    E. Beautiful Subarrays 字典树
    反素数 -- 数学
    Codeforces Beta Round #79 (Div. 1 Only) B. Buses 树状数组
  • 原文地址:https://www.cnblogs.com/invisible2/p/7792421.html
Copyright © 2011-2022 走看看