[C++] OOP - Access Control and Class Scope
Access Control And Inheritance
Protected Member
Like private, protected members are unaccessible to users of the class
Like public, protected members are accessible to members and friends of classes derived from this class.
Members and friends of a derived class can access the protected members only on the base class that are embeded inside the derived type object; they have no special access to a ordinary object of the base class.
class Base{
protected:
int prot_item;
};
class Sneaky : public Base{
friend void clobber(Sneeky &);
friend void clobber(Base &);
int j;
};
void clobber(Sneaky & s) { s.prote_item =;} // ok
// error: clobber cannot access the protect member in Base;
void clobber(Base & b) { b.prot_item = ;}
public, private and protect inheritance
class Base{
public:
void pub_mem();
protected:
int prot_mem;
private:
char priv_mem;
}
struct Pub_Derv : public Base{
// ok. derived class can access protected members
int f() { return prot_mem; }
// error. private members are inaccessible to derived class
char g() { return priv_mem; }
}
struct Priv_Derv : private Base{
// private derivation do not affect access in the derived class
int f1() { return prot_mem; } // ok
char g2() {return priv_mem; } // error
}
The derivation access specifier have no effect on whether a member of derived class may access to the members of its own direct base class. Access to the members of the base class is conrolled by the access specifer in the base class itself.
Both Pub_Deriv and Priv_Derv have access to the protected member prot_mem. Neither of them may access the private member Priv_mem.
The derivation access specifier is to control the access that users of the derived class - including other classed derived from the derived class - have to the members inherited from base.
Pub_Derv d1; // members inherited from Base remain the same access specifer
Priv_Derv d2; // members inherited from Base are private
d1.pub_mem(); // ok. pub_mem is public in the derived class
d2.pub_mem(); // error. pub_mem is private in the derived class
The derivation access specifier used by the derived class also control the access from classed that inherited from the derived class.
struct Drived_from_public : public Pub_Derv{
// ok. Base::prot_mem remain protected in Pub_Derv
int use_base(){ return prot_mem; }
};
struct Derived_from_private : public Priv_Derv{
// error. Base::prot_mem is private in Priv_Derv
int use_base() { return prot_mem; }
};
Had we defined another class, say, Prot_Derv, that use protected inheritance, the public members of base would be protected members in that class. Users of prot_Derv would have no access to Pub_mem, but members and friends of Prot_Derv have access to the inherited members.
Assumig D inherits from B.
User code may use the derived-to-base conversion only if D inherits publicly from B. User code may not use the conversion if D inherits from B using either protected or private.
An implementation member should be protected if it provides operation or data that a derived class will use in its own implementation. Otherwise, implementation member should be private.
friendship and inheritance
Just as friendship is not transitive, friendship is also not inherited.
Friends of the base type have no special access to the members of the derived class. Friends of the derived type also have no special access to the members of the base type.
class Base{
// remain other members as before
friend class pal;
};
class Pal{
public:
int f(Base b) { return b.prot_mem; } // ok
int f2(Sneaky s) { return s.j; } // error. Pal is not friend of Sneaky
int f3(Sneaky s) { return s.prot_mem; } // ok!!!
}
The fact that f3 is legal may be surprising, but it follows the notion that each class controls access to its own members. Pal is a friend of Base. So it also have access to the Base object that embeded in an object of type derived from Base.
Tony: when a friend of a base type access to a member through the derived class of the base type, need to consider 1)whether this member is inherited from the base, 2) if yes, the actual access level of the member in the base type.
When a class make another class a friend, it is only that class to which friendshp is granted. The base class class of, and the derived class from, the friend have no special access to the members of the befriending class.
class D2 : public Pal{
public:
// error. friendship is not inherited.
int mem(Base b) { return b.prot_mem; }
};
Exempting Individual Members
Sometimes we need to change the access level to a mambers that a class inherits. We can do so by providing a using declaration.
class Base{
public:
size_t size() const { return n; }
protected:
size_t n;
};
class Derived : private Base{
public:
using Base::size;
protected:
using Base::n;
};
Tony: private derivation do not support implicit derived-to-base conversion; then it also do not support polymorphism. So, privevation derivation is not a common use in OOP. The effective use of changing access level to members in C++ is similar to that in Java.
Class Scope under Inheritance
class Disc_quote: public Quote{
public:
pair<size_t, double> discount_policy const {
return {quantity, discount};
}
// other members as before
};
Bulk_quote bulk;
Bulk_quote * bulkP = &bulk; // static and dynamic type are the same
Quote * itemP = & bulk; // static andy dynamic type differ
bulkP->discount_policy(); // ok
itemP->discount_policy(); // error itemP has type Quote*
Even though bulk has the member named discount_policy, that members is not visible to through itemP. The type of itemP points to Quote, which means that the search for discount_policy start in class Quote.
Bulk_quote bulk;
cout << bulk->isbn();
The use of the isbn is resolved as follow:
Because we call isbn on an object of Bulk_quote, the research start in Bulk_quote. The name isbn is not found in that class.
Because Bulk_quote is derived from Disc_quote, the Disc_quote is searched next. The name isbn is not found.
Because Disc_quote is derived from Quote, The Quote is searched next. The name isbn is found in that class, the use of isbn is resolved to the isbn in Quote.
As usual, names defined in the inner scope(e.g. a derived class) hides the same names of outer scope(e.g. the base class).
We can use a hidden base-class memer by scope operator:
struct Derived : Base{
int get_base_mem() { return Base::mem; }
// ...
};
Aside from overriding inherited virtual function, a derived class should not reuse the names defined in its base class.
Functions defined in a derived class do not override members defined in its base class. If a memebers in a derived class has the same name as the base member, then the base member is hidden even if they have different parameter list
struct Base{
int memfcn();
};
struct Derived : Base{
int memfcn(int); // hides memfcn in the base
};
Base b;
Derived d;
b.memfcn();
d.memfcn();
d.memfcn(); // error: memfcn with no argument is hidden
d.Base::memfcn(); // ok
To resolve this call, look for name memfcn in Derived. The name is found, then compiler stop further search. The version of memfcn in Derived expected an int argument. This call provide no argument; it is in error.
class D1{
public:
int fcn(int);
};
class D2 : public D1{
public:
int fcn(int); // nonvirtual function hide D1::fcn(itn)
};
D2 d2Obj;
D1 *p1 = &d2Obj;
D2 *p2 = &d2Obj;
p1->fcn(); // statically bound, call D1::fcn(int)
p2->fcn(); // statically bound, call D2::fcn(int)
When we call an nonvirtual function, the version that is called depends only on the static type of the object. The dynamic type is ignored.
Override Overload Functions
If a derived class want to make ll overload functions of base available through its type, then it must override all of them or non of them.
Instead of Override every base class version that it inherits, a derived class can provide using declaration for the overload function. A using declaration only specifier a name. Thus, using declaration for a base members adds all instances of the overload function into the scope of the derived class.
Reference:
C++ Primer, Fifth Edition, chapter 15 Object-Oriented Programming
[C++] OOP - Access Control and Class Scope的更多相关文章
- Oracle Applications Multiple Organizations Access Control for Custom Code
档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...
- [认证授权] 6.Permission Based Access Control
在前面5篇博客中介绍了OAuth2和OIDC(OpenId Connect),其作用是授权和认证.那么当我们得到OAuth2的Access Token或者OIDC的Id Token之后,我们的资源服务 ...
- Role-based access control modeling and auditing system
A role-based access control (RBAC) modeling and auditing system is described that enables a user to ...
- Browser security standards via access control
A computing system is operable to contain a security module within an operating system. This securit ...
- A GUIDE TO UNDERSTANDINGDISCRETIONARY ACCESS CONTROL INTRUSTED SYSTEMS
1. INTRODUCTION The main goal of the National Computer Security Center is to encourage the widespr ...
- Enhancing network controls in mandatory access control computing environments
A Mandatory Access Control (MAC) aware firewall includes an extended rule set for MAC attributes, su ...
- Access control differentiation in trusted computer system
A trusted computer system that offers Linux® compatibility and supports contemporary hardware speeds ...
- Risk Adaptive Information Flow Based Access Control
Systems and methods are provided to manage risk associated with access to information within a given ...
- Extensible Access Control List Framework
Methods, systems, and products for governing access to objects on a filesystem. In one general embod ...
随机推荐
- idea 聚合项目里如果子项目引用不到父类的maven应用
idea 聚合项目里如果子项目引用不到父类的maven应用,可以点看子类pom.xml文件,然后右键---->maven----->Reimport即可 点击右边子项目的maven---& ...
- datatable去掉表头默认排序
禁用排序:"ordering":false 某一列禁用排序:"orderable":false 以某一列排序:"order":[[x,&qu ...
- python字符编码转换说明及深浅copy介绍
编码说明: 常用编码介绍: ascii 数字,字母 特殊字符. 字节:8位表示一个字节. 字符:是你看到的内容的最小组成单位. abc : a 一个字符. 中国:中 一个字符. a : 0000 10 ...
- 定义一个大数组时,出现错误,程序进入HardFault_Handler中断
在原子的串口程序前加了几个数组定义,加了个对数组处理的函数,出现了HardFault_Handler的错误,不知道怎么解决!!! 因为局部变量是存放在栈区的,而全局变量在全局区(静态区),如果栈区较小 ...
- python基础 - 字符串与列表的基本操作方法
# v = 11# data = v.bit_length()# print(data) # a = 'ABCDEFGHIJK'# print(a[0])# print(a[10]) # print( ...
- Python知乎热门话题爬取
本例子是参考崔老师的Python3网络爬虫开发实战写的 看网页界面: 热门话题都在 explore-feed feed-item的div里面 源码如下: import requests from py ...
- python网络编程之协程
本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质:切换+保存状态 cpu正在运行一个任务,会在两种情况下切走去执行其他的 ...
- 详解CSS中的几种长度px、em、pt
说说css的几种距离吧,大致有px.em.pt.pc.in.mm.cm.ex八种,其中最常见到的是px,我还见到过的有ex和mm.cm,当然后两个在当年见的更多. 其实px,我们最熟悉,而在电脑上也应 ...
- Solr第一讲——概述与入门
一.solr介绍 1.什么是solr Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器.Solr可以独立运行在Jetty.Tomcat等这些Serv ...
- Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)
Caliburn.Micro 杰的入门教程1(翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程3, ...