博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
In c++ access control works on per-class basis not on per-object basis.
阅读量:4636 次
发布时间:2019-06-09

本文共 2020 字,大约阅读时间需要 6 分钟。

#ifndef MYTIME_H #define MYTIME_H   class MyTime { private:     int m_hour;     int m_minute; public:     MyTime(int hour,int minute=0);     ~MyTime();       MyTime operator+(const MyTime & time) const;     void Show();       //int GetHour() const;     //int GetMinute() const; };   #endif
#include "MyTime.h" #include 
MyTime::MyTime(int hour,int minute) { m_hour=hour; m_minute=minute; }; MyTime::~MyTime() {}; MyTime MyTime::operator+(const MyTime & time) const{ //MyTime sum // =MyTime( // time.GetHour()+m_hour+(time.GetMinute()+m_minute)/60, // (time.GetMinute()+m_minute)%60 // ); //return sum; MyTime sum =MyTime( time.m_hour+m_hour+(time.m_minute+m_minute)/60, (time.m_minute+m_minute)%60 ); return sum; }; void MyTime::Show() { std::cout<<"Hour = "<
<<" , Minute = "<
<
Because that's how it works in c++.In c++ access control works on
per-class basis not on per-object basis.
Access control in c++ is implemented as a static,compile-time feature.I think it is rather obvious that it is not really possible to implement any meaningful per-object access control at compile time.only per-class control can be implemented that way.
some hints of per-object control are present in protected access specification,which is why it even has its own dedicated chapter in the standard(11.5).But still any per-object features described there are rather rudimentary.Again,access control in c++ is meant to work on per-class basis.
Your "it is not really possible to implement any meaningful per-object access control at compile time". Why not? In void X::f(X&x), the compiler is easily capable of distinguishing this->a and x.a. It's not (always) possible for the compiler to know that *this and x are actually the same object if x.f(x) is invoked, but I could very well see a language designer find this OK.

转载于:https://www.cnblogs.com/hongjiumu/p/3500790.html

你可能感兴趣的文章
Some configure
查看>>
.net core 中的[FromBody]
查看>>
json_encode时中文编码转正常状态
查看>>
流量调整和限流技术 【转载】
查看>>
Axure 全局辅助线(转)
查看>>
正由另一进程使用,因此该进程无法访问此文件。
查看>>
27-THREE.JS 平面
查看>>
以太网基础(转)
查看>>
tp5+linux+apache php7.1.30环境下,上传图片报错:mkdir():permission denied
查看>>
单片AT89C2051 + SD卡 + 3310LCD = 音乐播放器
查看>>
dp cf 20190615
查看>>
1 线性空间
查看>>
MVC 中的 ViewModel
查看>>
机器学习
查看>>
begin.lydsy 入门OJ题库:1104:纯粹合数
查看>>
DataCleaner 3.1.1 发布,数据质量分析管理
查看>>
memcached和redis的区别和应用场景
查看>>
【sping揭秘】6、IOC容器之统一资源加载策略
查看>>
转:AbstractQueuedSynchronizer的介绍和原理分析
查看>>
修改或隐藏Nginx的版本号
查看>>