Home  >  Article  >  Database  >  当虚函数的访问权限access control改变时

当虚函数的访问权限access control改变时

WBOY
WBOYOriginal
2016-06-07 15:37:381432browse

#include iostream using namespace std; class Base { public: virtual void show() { coutBaseendl; } }; class De:public Base { private: virtual void show() { coutDeriendl; } }; void main() { Base *p = new De; p-show(); //OK,Output : Deri }

#include
using namespace std;

class Base
{
public:
 virtual void show() {
  cout }
};

class De:public Base
{

private: 
 virtual void show() {
  cout }
};

void main()
{
 Base *p = new De;
 p->show();  //OK,Output : Deri
}
当把基类和派生类的public,private互换时,
error #308: function "Base::show" is inaccessible
   p->show();
  
可见类虚拟成员函数访问性是由调用该函数的指针决定的
(本程序中是由p的类型)

#include
using namespace std;

class Base
{
public:
 virtual void show() {
  cout }
};

class De:public Base
{

private: 
 virtual void show() {
  cout }
};

void Test(Base& p)
{
 p.show();
}

void main()
{
 Base *p = new De;
 p->show();    //OK output : Deri

 Base ba;
 Test(ba);    //OK : base

 De da;
 Test(da);    //OK : Deri
}
 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:cocos2dx中lua实现继承详解Next article:addr2line探秘