简体   繁体   English

Typedef C++ 中的枚举值

[英]Typedef an enum value in C++

I have a base class that has an enum value that I am using in the derived class.我有一个基础 class,它有一个我在派生的 class 中使用的枚举值。 The base class is a Table<> and the derived class is a Matrix<> .基础 class 是Table<> ,派生的 class 是Matrix<> Now the enum value in Table<> is TABLE_SIZE which is used in the Matrix<> class.现在Table<>中的枚举值为TABLE_SIZE ,用于Matrix<> class。 Since TABLE_SIZE does not make a lot of sense (it does a little,) in the matrix class, I thought I would typedef it to something more consistent with Matrix<> ( MATRIX_SIZE ).由于TABLE_SIZE在矩阵类中没有多大意义(它有一点作用),我想我会将它的类型定义为与Matrix<>MATRIX_SIZE )更一致的东西。

typedef TABLE_SIZE MATRIX_SIZE;

That didn't work, which was a bit surprising.这没有奏效,这有点令人惊讶。 I am guessing I can't typedef the value because the enumeration is a type but not the values (not sure if that is a correct observation)?.我猜我不能 typedef 值,因为枚举是一种类型而不是值(不确定这是否是正确的观察)? So, now the question is, how-do-I/can-I accomplish the above?所以,现在的问题是,我如何/我可以完成上述任务?

EDIT: One thing I forgot to mention is that I do not want the Matrix class to increase in size (yes I realize it is a tiny increase and wouldn't matter for most people, in my case, it does).编辑:我忘了提到的一件事是我不希望 Matrix class 的尺寸增加(是的,我意识到这是一个微小的增加,对大多数人来说并不重要,就我而言,确实如此)。

You can define an enumeration: enum { MATRIX_SIZE = TABLE_SIZE };你可以定义一个枚举: enum { MATRIX_SIZE = TABLE_SIZE };

typedef is for types, not values. typedef用于类型,而不是值。 Use利用

static const size_t MATRIX_SIZE = TABLE_SIZE;

(Assuming size_t is the correct type here.) (假设size_t是正确的类型。)

Even better, just rename TABLE_SIZE to SIZE or size ;更好的是,只需将TABLE_SIZE重命名为SIZEsize since it's a member, it can hardly be confused with any of the other size s in your program.因为它是一个成员,所以它几乎不会与您程序中的任何其他size混淆。

I'm going to take a different tack here.我将在这里采取不同的策略。

Since TABLE_SIZE does not make a lot of sense (it does a little,) in the matrix class,由于 TABLE_SIZE 在矩阵类中没有多大意义(它有点作用),

Rhetorical question: If it doesn't make sense, why are you doing it?反问:如果没有意义,你为什么要这样做? The answer is that there is some reason you need your Matrix to be the same size as the base Table.答案是出于某种原因,您需要矩阵与基表的大小相同。 So why hide the fact?那么为什么要隐瞒事实呢? Be explicit.明确。 Don't obscure the fact the matrix size and table size are one and the same.不要掩盖矩阵大小和表格大小相同的事实。 Think about the poor future maintainer (probably you), who has to go plow through that extra level of indirection (probably with no comment as to why).想想可怜的未来维护者(可能是你),他们必须 go 犁过那个额外的间接级别(可能没有评论为什么)。

Not only should you be explicit, that you are intentionally making the matrix size the same as the underlying table size is a design decision that is worthy of a comment.您不仅应该明确,而且有意使矩阵大小与基础表大小相同,这是一个值得评论的设计决策。

Always program as if the future maintainer is a homicidal maniac who knows where you live.总是像未来的维护者是一个知道你住在哪里的杀人狂一样编程。

typedef is the keyword for declaring a type alias , but TABLE_SIZE is not a type. typedef是声明类型别名的关键字,但TABLE_SIZE不是类型。

You could use the preprocessor:您可以使用预处理器:

#define MATRIX_SIZE TABLE_SIZE

(Waiting for the unsubstantiated "macros are evil." claims, Go on. I dare you.) (等待未经证实的“宏是邪恶的。”声称,Go on。我敢说。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM