c++ - 如何在 C++ 中访问受保护的运算符成员
问题描述
如何在 C++ 中访问受保护的运算符成员
class A {
char* m="ABCD";
protected:
const char& operator[](int i)const{ return m[i]; };
public:
A();
};
class B : Public A {
char* D;
Public:
B():A(){};
void display(){ ///////////what should I do here }
};
如果我有这段代码,并且我想在派生类中使用运算符 [],例如我需要在派生类的显示中显示 [0]=A。这里有什么解决办法。
解决方案
this
您可以从成员函数中取消引用此对象并使用[]
运算符,即:
void display(){ std::cout << (*this)[1]; }
推荐阅读
- c# - 通过 System.Reflection 获取类型:尝试强制转换时出错
- kubernetes - 在 Kubernetes 的 pod 中为单个容器使用非持久卷
- javascript - 无法使用 addEventListner 添加“活动”类
- javascript - 是否可以在浏览器中拦截第三方 POST 请求?
- wpf - 如何在wpf的itemcontrols中设置命令的数据上下文
- javascript - 使用 CI 从表中删除数据
- python - Docker 无法创建 conda 虚拟环境
- java - 用于在动作子句中传递参数的字符串连接
- git - 当 git repo 中有提交时,用于 codepileline 的 Github webhook 不会触发
- reactjs - 反应功能组件的 setState 未应用更改