c++设计查询
我正在用 C++ 设计一个框架,它应该提供基本功能并充当其他派生系统的接口。
#include <stdio.h>
class Module
{
public:
virtual void print()
{
printf("Inside print of Module\n");
}
};
class ModuleAlpha : public Module
{
public:
void print()
{
printf("Inside print of ModuleAlpha\n");
}
void module_alpha_function() /* local function of this class */
{
printf("Inside module_alpha_function\n");
}
};
class System
{
public:
virtual void create_module(){}
protected:
class Module * module_obj;
};
class SystemAlpha: public System
{
public:
void create_module()
{
module_obj = new ModuleAlpha();
module_obj->print(); // virtual function, so its fine.
/* to call module_alpha_function, dynamic_cast is required,
* Is this a good practice or there is some better way to design such a system */
ModuleAlpha * module_alpha_obj = dynamic_cast<ModuleAlpha*>(module_obj);
module_alpha_obj->module_alpha_function();
}
};
main()
{
System * system_obj = new SystemAlpha();
system_obj->create_module();
}
编辑代码使其更加合乎逻辑,并且可以立即编译。问题是,是否有更好的方法来设计这样的系统,或者dynamic_cast是唯一的解决方案。此外,如果有更多派生模块,则对于类型转换,基 Module 类中需要一些智能。
I am designing a framework in c++ which is supposed to provide basic functionality and act as interface for the other derived systems.
#include <stdio.h>
class Module
{
public:
virtual void print()
{
printf("Inside print of Module\n");
}
};
class ModuleAlpha : public Module
{
public:
void print()
{
printf("Inside print of ModuleAlpha\n");
}
void module_alpha_function() /* local function of this class */
{
printf("Inside module_alpha_function\n");
}
};
class System
{
public:
virtual void create_module(){}
protected:
class Module * module_obj;
};
class SystemAlpha: public System
{
public:
void create_module()
{
module_obj = new ModuleAlpha();
module_obj->print(); // virtual function, so its fine.
/* to call module_alpha_function, dynamic_cast is required,
* Is this a good practice or there is some better way to design such a system */
ModuleAlpha * module_alpha_obj = dynamic_cast<ModuleAlpha*>(module_obj);
module_alpha_obj->module_alpha_function();
}
};
main()
{
System * system_obj = new SystemAlpha();
system_obj->create_module();
}
Edited the code to be more logical and it compiles straight away. The question is, that is there a better way to design such a system, or dynamic_cast is the only solution. Also, if there are more derived modules, then for type-casting, there is some intelligence required in the base Module class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果
Derived
是Base
的唯一具体实例,您可以使用static_cast
代替。就我个人而言,我为每个专门的类定义了一个函数,例如 MyCast。我定义了四个重载变体,以便我可以向下转换 const 和非 const 指针和引用。例如:
Derived2 和 Base2 也是如此。
拥有这四个的最大优点是,您不会意外更改常量,并且无论您有指针还是引用,都可以使用相同的构造。
当然,您可以用宏替换
static_cast
,并在调试模式下使用dynamic_cast
,而在发布模式下使用static_cast
。此外,上面的代码可以轻松地包装到宏中,从而可以轻松地批量定义函数。
使用此模式,您可以将代码实现为:
If
Derived
is the only concrete instance ofBase
you could usestatic_cast
instead.Personally, I define a function, like
MyCast
for every specialized class. I define four overloaded variants, so that I can down-cast const and non-const pointers and references. For example:And likewise for Derived2 and Base2.
The big advantage in having all four is that you will not change constness by accident, and you can use the same construct regardless if you have a pointer or a reference.
Of course, you could replace
static_cast
with a macro, and usedynamic_cast
in debug mode andstatic_cast
is release mode.Also, the code above can easily be wrapped into a macro, making it easy to batch-define the functions.
Using this pattern, you could then implement your code as:
如果
Base
不包含base_obj
对象,而是通过虚拟方法获取引用,那么设计会变得更加清晰。Derived
应包含一个Derived2
对象,例如:如果您担心性能,请在
Base
的构造函数中传递引用。The design gets much cleaner if
Base
does not contain thebase_obj
object, but rather gets a reference via a virtual method.Derived
should contain aDerived2
object, like:If you are worried about performance, pass the reference in the constructor of
Base
.我采用了您的代码及其许多编译错误并尝试简化它。这就是你想要达到的目标吗?它将编译。
I took your code with its many compile errors and tried to simplify it. Is this what you are trying to acheive? It will compile.