扩展枚举?

发布于 2024-11-02 09:02:55 字数 235 浏览 1 评论 0原文

假设我创建了一个枚举,但最终有人想向该枚举添加项目,该怎么办? 例如:

// blah.hpp

enum PizzaDressing {
    DRESSING_OLIVES = 0,
    DRESSING_CHEESE = 1
};

在我的 FunkyPizza 课上,可能会有胡椒配料。

那么如何在不明显修改原始枚举的情况下添加辣椒呢?

谢谢。

Say I create an enum but eventually someone wants to add items to that enum, what does one do?
ex:

// blah.hpp

enum PizzaDressing {
    DRESSING_OLIVES = 0,
    DRESSING_CHEESE = 1
};

and in my FunkyPizza class, there might be pepper toppings.

So how could I add peppers without obviously modifying the original enum?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我还不会笑 2024-11-09 09:02:55

这最接近您想要的:基枚举类继承

This is closest to what you want: Base enum class inheritance

盛夏尉蓝 2024-11-09 09:02:55

由于枚举通常在编译器中被处理为某种大小的 int ,因此您所要做的就是稍后 make

enum PizzaDressing
{
    Olives = 0,
    Cheese = 1,
    Pepperoni = 2
};

或者您可以允许它计数

enum PizzaDressing
{
    Olives = 0,
    Cheese = 1,
    Pepperoni
};

您可以,如果由于某种原因这两者都不可接受,则可以使用 math (Cheese + 1)。
您几乎可以用任何可以使用数值的方式来使用枚举。

请注意,您使用的枚举器通常由编译器烘焙到代码中,它不会显示为名称,而只是显示为值。因此,稍后修改(扩展)枚举器不会影响已构建的代码。

我认为在另一个枚举器中使用枚举并进行强制转换是合法的语法,但我从未尝试过。这可能有效,但有点难看:

enum PizzaDressing
{
    Olives = 0,
    Cheese = 1
};

enum OtherPizzaDressings
{
    Start = (OtherPizzaDressings)PizzaDressing::Cheese;
    Pepperoni
};

Since enums are typically handled as some size of int in the compiler, all you have to do is later make

enum PizzaDressing
{
    Olives = 0,
    Cheese = 1,
    Pepperoni = 2
};

or you could allow it to count

enum PizzaDressing
{
    Olives = 0,
    Cheese = 1,
    Pepperoni
};

You could, if neither of those is acceptable for some reason, use math (Cheese + 1).
You can play around with the enum in almost any way you could with a numeric value.

Note that the enumerator you use is typically baked into the code by the compiler, it doesn't show up as its name, simply value. Thus, modifying (extending) the enumerator later on will not effect code that has been built.

I think it's legal syntax to use an enumeration in another enumerator, with casts, but I've never tried it. This may work, but is kind of ugly:

enum PizzaDressing
{
    Olives = 0,
    Cheese = 1
};

enum OtherPizzaDressings
{
    Start = (OtherPizzaDressings)PizzaDressing::Cheese;
    Pepperoni
};
请帮我爱他 2024-11-09 09:02:55

这被称为“动态枚举”。据我所知,C++ 中不存在这样的东西。但是,由于我们使用的是 C++ 而不是 C,因此您可以执行以下操作:

#include <string>
#include <map>

std::map<std::string, int> myMap;
myMap["DRESSING_OLIVES"] = 0;
myMap["DRESSING_CHEESE"] = 1;
myMap["PEPPER_TOPPING"] = 2;

That would be known as a "dynamic enum". To the best of my knowledge, nothing like this exists in C++. However, since we're using C++ and not C, you could do something like this:

#include <string>
#include <map>

std::map<std::string, int> myMap;
myMap["DRESSING_OLIVES"] = 0;
myMap["DRESSING_CHEESE"] = 1;
myMap["PEPPER_TOPPING"] = 2;
刘备忘录 2024-11-09 09:02:55

您无法动态修改枚举,因为它仅定义在编译时解析的新类型。它们是程序员的助记符,在编译时它们被转换为数字。

也就是说,您可以使用原始枚举未使用的任何数字来表示您想要的任何内容:

PizzaDressing a;
a = (PizzaDressing)5;

You can't dynamically modify an enum, because it only defines a new type resolved at compile time. They are mnemonics for the programmer, at compilation they are translated to numbers.

That said, you can use any number not used by the original enum to represent whatever you want:

PizzaDressing a;
a = (PizzaDressing)5;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文