C++虚函数访问权限的改变
从这里也说明了函数的覆盖或者说重定义跟前面的访问权限修饰没多大关系
//Base.h
#pragma once #include <iostream> using namespace std; class Base { public: Base(void){} ~Base(void){} virtual void fun(){cout<<"This is Base::fun"<<endl;} virtual void func()=0; };
//Son.h
#pragma once #include "base.h" class Son :public Base { public: Son(void){} ~Son(void){} virtual void fun(){cout<<"This is a son function"<<endl;} protected: virtual void func(){cout<<"what a fuck day it is"<<endl;} };
//T.h
#pragma once #include "son.h" class T : public Son { public: T(void){} ~T(void){} void xy() { Base *b = new Son(); b->func(); } };
//main.cpp
#include "testInline.h" #include "Son.h" #include "T.h" void main() { Base* pB; Son* pS = new Son(); pB = pS; pB->fun(); T tst; tst.xy(); }
结果:
This is a son function
what a fuck day it is
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。