c++虛函數重寫的許可權問題

來源:互聯網
上載者:User

標籤:ios   namespace   str   問題   ase   end   函數重寫   private   理解   

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,...)



 

c++虛函數重寫的許可權問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.