如何删除继承的私有char*属性?(例如:在析构函数中)
How to delete an inherited private char* attribute? (example : in a destructor)
我试图删除这个程序中继承的char*
属性:
class A {
public :
// Functions, constructors and such
private :
char* attribute;
}
在B.h #include "A.h"
class B : public A {
public :
B(const char* _attribute, int s) : A(_attribute) {setSpeed(s);}
~B()
private :
int speed;
}
在析构函数中使用delete []:
B::~B() {
delete [] attribute;
}
但是我得到这个错误:' char*A::attribute'是私有的
在A的析构函数(~A()
)中,我使用相同的"destroy[]属性",它可以工作…
因为它是A私有的,所以A应该是负责删除它的类。
你不应该在B中删除它,这违反了基本的封装。B应该只负责删除自己的属性
你在类A中的属性被定义为private,所以它不被类b继承。它不存在于类b中,这就是为什么你不能删除它。
尝试使用protected.
不能从派生类访问基类的私有变量。如果您希望从派生类访问它,则需要使其受保护。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过指向指针数组的指针访问子类的属性
- 将数字转换为字母(例如:123 转换为一二三)
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- MSVC是否支持C++11样式的属性而不是__declspec
- 如何计算数据类型的范围,例如int
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- Qt Quick-如何仅从c++代码与qml属性交互
- 用概念检查属性的类型
- C++如何将同一类的实例作为属性
- 这是否符合C++标准:双响双响,例如!!(-0.0).
- 按多个属性排序
- 主函数参数的属性
- 我可以控制鼠标属性,例如点击速度
- 是否可以为现有类的实例绑定新属性,例如 c++ 中的 JavaScript?
- 如何删除继承的私有char*属性?(例如:在析构函数中)