c++ - 如何删除继承的私有(private) char* 属性? (例如 : in a destructor)

标签 c++ inheritance attributes char

我正在尝试删除此程序中继承的 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' 是私有(private)的

在 A 的析构函数 (~A()) 中,我使用相同的“destroy [] attribute”并且它有效...

最佳答案

既然是A私有(private)的,那么A应该是负责删除它的类。

你不应该在 B 中删除它,这违反了基本封装。 B 应该只负责删除自己的属性。

关于c++ - 如何删除继承的私有(private) char* 属性? (例如 : in a destructor),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14134665/

相关文章:

c++ - Bagon 先生为 Mean Shift Segmentation 提供的 edison_wrapper_mex.cpp 的 Mex 文件在 MATLAB 2014a 中不起作用

c# - 如何在 UML 中正式记录 C# 属性?

openid 属性交换

PHP 简单 html DOM 从 html 标签中删除所有属性

c++ - 数值转换

c++ - boost::fusion解析长字符串导致栈溢出

C++对象,动态和静态的区别

typescript - 如何定义将类型作为强类型参数的 Typescript 函数

python - 用 Super 构建合适的 Python Facade 类?

C++ : Calling a child method from parent instantiation