如何为我的班级创建一个好的界面
How to create a good interface for my class?
我可以用来组织我的代码吗?
我使用C 。
- 我有一个基类命令
- 从命令类得出的数十个类
- 类交易,存储命令数组(可以更改)
使用当前方法,交易接口的用户应执行
之类的事情template <typename Base, typename T>
inline bool instanceof(const T *ptr) {
return typeid(Base) == typeid(*ptr);
}
Transaction tx;
// here I want to process all commands
for(int i=0; i < N; i++){
if(instanceof<AddPeer>(tx.get(i)) {
// ... process
}
if(instanceof<TransferAsset>(tx.get(i)) {
// ... process
}
... for every type of command, but I have dozens of them
}
class Command;
class TransferAsset: public Command {}
class AddPeer: public Command {}
// dozens of command types
class Transaction{
public:
// get i-th command
Command& get(int i) { return c[i]; }
private:
// arbitrary collection (of commands)
std::vector<Command> c;
}
为什么,简单地,命令没有在派生类中实现的虚拟纯方法?这样的东西:
class Command
{ virtual void process () =0;};
class TransferAsset: public Command
{
void process ()
{
//do something
}
};
class AddPeer: public Command
{
void process ()
{
//do something
}
};
您的代码可能是:
Transaction tx;
// here I want to process all commands
for(int i=0; i < N; i++)
{
tx.get(i)->process();
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 将下一个(),hasNext()迭代界面转换为begin(),end()接口
- 如何为我的班级创建一个好的界面
- 获取一个文件的名称方法 - 自定义界面 - Microsoft Visual Studio 2010
- 在C++中实现一个基于文本命令的用户界面
- 是否可以创建一个可以在ios移动平台和windows桌面平台之间运行的应用程序,而无需使用web界面
- 用户界面-我应该使用哪个工具包为现有的c++代码制作一个易于编辑、外观现代的GUI
- 用户界面-如何去gui一个复杂的纠缠c++ /Qt4应用程序
- 在c++中创建一个井字游戏的图形界面