如何使用动态绑定继承适当的类
How to inherit appropriate class using dynamic binding?
我有一个具有抽象基础class FILEPARSER
的程序,该程序具有两个虚拟方法read()
和print()
。从此基类继承的两个类是:XMLPARSER
和将实现方法的CONFIGPARSER
。
主程序应该接受文件类型"config"或"XML"并继承该类型的适当类?
从命令行接受选项。
你必须显式构造正确的类(伪代码):
FileParser* parser = 0;
ParserType type = //retrieve the type you need
switch( type ) {
case ParserTypeConfig:
parser = new ConfigParser();
break;
case ParserTypeXml:
parser = new XmlParser();
break;
default:
//handle error
};
//then at some point you use the created object by calling virtual functions
parser->read(blahblahblah);
parser->print();
// and then at some point you delete the heap-allocated object
delete parser;
parser = 0;
当然,您应该使用智能指针来处理堆分配的对象。
相关文章:
- 通过继承类使用来自不同命名空间的运算符
- C++ 多重继承:使用基类 A 的实现实现基类 B 的抽象方法
- 从模板类继承并使用继承其构造函数
- 类继承和使用指针
- 在构造函数继承中使用默认构造函数
- 不允许在私有继承中使用基对象
- 带有递归继承和使用声明的Variadic模板
- 继承:子项使用父虚函数而不是自己的函数
- 使初始化列表与 C++ 中的继承一起使用
- clang/g++ 与私有继承和使用声明的区别
- 继承中使用的受保护变量
- 使用unordered_map成员函数指针和继承,使用 std:function
- 为什么需要在模板继承中使用范围解析
- 继承类使用非默认构造函数初始化自定义类
- 在继承中使用delete的意外行为,基指针对象指向派生程度最高的类
- 为什么继承失败(使用超类的方法)
- 这种虚拟继承的使用模式是否"method injection"已知范式?
- 继承和使用派生函数
- (初学者)c++构造函数和方法的继承,使用一个Java示例作为起点
- "Proper" C++中多重继承的使用