Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-07 15:37:38
Original
1432 people have browsed it

#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
}
 

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!