English 中文(简体)
用户界面功能使用 enum 类型是否是一个好主意?
原标题:Is using enum types for user interface functions a good idea?
  • 时间:2012-05-23 11:42:55
  •  标签:
  • c++
  • c++11

假设我正在构建一个简单的类 来打印屏幕上的文字 并且可以改变文字的颜色

myclass a("this is the text");
a.setColor("green");

我还在学习C++,最近我被介绍到 enum ,并觉得我会尝试一下。 我在想,在像上述 setColor 这样的界面函数中使用 enum 类型是否是一种良好做法?

在界面功能中使用 < code>enum 类的利弊是什么?是否有比其适用性更强、更差的情况?

如果我想结合属性呢?

a.setAttribute("bold reverse");

I don't know 界面 是否是我想描述的正确术语: 班级用户最终会使用的函数 。

最佳回答

在您的情况中,有两种好处:

  • No need to parse the string at run-time, leading to higher efficiency. You can use an enum variable directly in a switch statement.
  • An enum is (to some extent) self-documenting, the user of your code has to work hard to provide an invalid value.

一种潜在的“ 劣势” 是颜色字符串来源于例如运行时用户输入( 已经输入到文本框中 ) 。 您需要剖析此字符串并将其转换为 enum 。 但这并不是一个缺点, 因为无论如何您都需要这样做 。 最佳做法是用户界面逻辑尽早验证字符串并将其转换为 enum 。

如果我想结合属性呢?

我至少可以想到三种选择:

  • Use multiple calls to setAttribute.
  • Pass an array of attributes.
  • Define each enum value to be a power-of-two, and then you can combine enums with |.
问题回答

是的,在此情况下使用 enum 似乎比实际字符串好。

一种明显的优势——打字能力强。

如果 setColor 接受 color , 和您的情况一样,您可以:

 a.setColor("horse");

在运行时只能发现错误。

如果 secolor coolor 为参数:

 a.setColor(eGreen);
 a.setColor(eRed);

将汇编,但

 a.setColor(eHorse);

不会。

元件绝对比字符串更明确。 至于数值的搭配, 您可以使用一些小小的拼凑来完成这项工作 。 设置您的元件的值增加两个, 然后可以一起操作 。

enum TextAttributes {
    Bold = 1,
    Italic = 2,
    Reverse = 4,
    StrikeThrough = 8,
    Underline = 16
};

TextAttributes attr = Bold | Reverse;




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签