关于继承枚举的设计问题
我正在构建一个框架,需要对类进行错误检查。我不能使用异常(不允许)。我决定创建一个由所有需要错误报告的类继承的类,该类具有诸如 ReportError() [对于类本身]和 GetError() [获取最后一个错误 - 由调用者使用]以及添加自定义错误的函数,称为 AddError(/* argument */)。该类有一个名为 FW_ERROR_TYPES 的枚举类型,其中包含一些最常见的默认错误类型。例如,FW_ERROR_INIT(未能正确初始化)、FW_ERROR_CTOR(构造对象期间发生错误)等。当类报告错误时,还可以添加可选的描述,该描述会被记录下来(有助于以后分析以查看发生了什么)错误的)。这一切都很好,并且非常适合我的目的。
当我想添加自定义错误时,问题就出现了。自定义错误无法添加为 FW_ERROR_TYPES,因为继承错误类的类不会继承枚举。我在设计错误类时并没有预见到这个问题。我最初的“设计”是,当调用 GetError() 来获取最后一个错误时,它将返回一个 FW_ERROR_TYPES,因此这样使用时会生成编译器错误:
if (SomeClass->GetError()) // OR
if (SomeClass->GetError()) == true)
也就是说,它将强制使用它的人专门检查针对 FW_ERROR_TYPES。当我发现继承错误类的类无法扩展 FW_ERROR_TYPES 枚举时,我不得不使用通用枚举,并被迫让 GetError() 返回一个无符号整数。我将 unsigned int 定义为 FW_ERROR_TYPE,以便至少有一些指示正在返回错误代码。
唷!所以我的问题是,我如何改进这个设计(或者放弃它并采用更好的设计)?有没有办法继承枚举类型?我查看了 codeproject 网站,发现了一个相当复杂的解决方案(如果一切都失败了,我会尝试一下)。任何帮助将不胜感激。
谢谢!
I am building a framework where I need error checking for classes. I cannot use exceptions (not allowed). I decided to make a class that is inherited by all the classes that need error reports which has functions such as ReportError() [for the class itself] and GetError() [to get the last error - used by the caller] as well as functions to add custom errors called AddError(/* arguments */). The class has an enumerated type called FW_ERROR_TYPES with a few default error types which are the most common. For example, FW_ERROR_INIT (failed to initialize properly), FW_ERROR_CTOR (error during construction of the object) etc. When a class reports the error, an optional description can be added as well, which is logged (useful for later analysis to see what went wrong). This is all well and good, and works rather nicely for my purpose.
The problem comes when I want to add custom errors. The custom errors cannot be added as FW_ERROR_TYPES because the enum is not inherited by the class which is inheriting the error class. I did not forsee this problem while I was designing the error class. My original 'design' was that when GetError() is called to get the last error, it will return a FW_ERROR_TYPES, so a compiler error will be generated when used like so:
if (SomeClass->GetError()) // OR
if (SomeClass->GetError()) == true)
That is, it will force whoever is using it to specifically check against FW_ERROR_TYPES. When I found out that classes inheriting the error class cannot extend FW_ERROR_TYPES enum, I had to go with a generic enum and was forced to have GetError() return an unsigned integer. I defined the unsigned int as FW_ERROR_TYPE so that at least there is some indication that an error code is being returned.
Phew! So my question is, how can I improve on this design (or scrap it and go with a better one)? Is there a way to inherit enumerated types? I looked at codeproject site and found a rather complicated solution (if all else fails I will give that a go). Any help would be greatly appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将错误值设置为类而不是枚举。您可以拥有一个基本错误类
FwError
,并具有诸如GetMessage()
之类的有用方法,然后拥有从该基本错误类继承的各种错误类型。当您必须检查返回的错误是否是某种类型的错误时,可以使用运行时类型信息来检查错误类型。如果需要创建新的错误类型,可以从错误基类或更具体的子类继承。如果调用者收到从未听说过的类型错误,多态性确保它仍然可以像基本FwError
一样处理它并打印有意义的消息。You could have your error value be a class instead of an enum. You could have a base error class
FwError
, with helpful methods likeGetMessage()
, and then have a variety of error types inheriting from that base error class. When you have to check whether an error returned is a certain type of error, you can use Run-Time Type Information to check error types. If you need to create a new type of error, you can inherit from the base error class, or a more specific subclass. If a caller gets an error of a type it has never heard of, polymorphism ensures it can still handle it like a baseFwError
and print a meaningful message.我发现这个问题的两个(“有点”复杂)解决方案:
http:// /www.codeproject.com/KB/cpp/InheritEnum.aspx
http: //www.codeproject.com/KB/cpp/ImprovedEnum.aspx
Two (a 'little' complicated) solutions that I have found to this problem:
http://www.codeproject.com/KB/cpp/InheritEnum.aspx
http://www.codeproject.com/KB/cpp/ImprovedEnum.aspx