有一个家长(关闭供公众使用):
class BaseElement {
protected:
unsigned _guid, _size;
char *_data;
bool _swap;
BaseElement(): _guid(0), _size(0), _data(nullptr), _swap(false) {}
public:
virtual bool Get(char *data, unsigned size);
//... other stuff
};
还有一个后代:
class SimpleBlock : public BaseElement {
unsigned char _track, _flags;
short _crpts;
public:
SimpleBlock(): _track(0), _flags(0), _crpts(0) { _guid = 0xA3; }
bool Get(char *data, unsigned size) override;
//... other stuff
};
child 方法的Get
作用相同,只是它将指针data
向前移动 4 个字节并从中获取其私有变量的值。Get
由于 4 行在子中复制父代码在某种程度上并不美观。
使用上面的类定义,是否可以在Get
子实现中调用相同的父方法并简单地添加操作_data
?
您只需要指定要调用其函数的类。例如: