静态枚举替代 [英] static enumeration alternative

查看:76
本文介绍了静态枚举替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我班级的私人部分写作

定义:


静态枚举TruncType {TRUNC_TOP,TRUNC_RIGHT,TRUNC_BOTTOM,TRUNC_LEFT} ;


但是C ++不允许这样做。上面的四个值是常数,并且静态只有
,并且只能由私有类方法使用。


我可以这样做:


static const int TRUNC_TOP;

static const int TRUNC_RIGHT;

等...


初始化它们是我的.cpp文件中的不同值。


但是使用枚举更好,或者特别适用于此目的的其他物品。什么是我想要的b $ b做什么的最佳方式?即,声明四个静态类常量,其值为

无关,除非作为区分标记。


谢谢,

cpp

I would like to write, within the private section of my class
definition:

static enum TruncType {TRUNC_TOP,TRUNC_RIGHT,TRUNC_BOTTOM,TRUNC_LEFT};

but C++ does not allow this. The four values above are constant and
static, and will be used only by a private class method.

I could just do:

static const int TRUNC_TOP;
static const int TRUNC_RIGHT;
etc...

initializing them to distinct values in my .cpp file.

But it would be nicer to use an enum, or somehting else specifically
suited to this purpose. What is the best way to what I''m trying to
do? Ie, declare four static class constants whose values are
irrelevant except as distinguishing markers.

Thanks,
cpp

推荐答案

" cppaddict" <他*** @ hello.com>写了...
"cppaddict" <he***@hello.com> wrote...
我想写一下,在我班级的私人部分定义:

静态枚举TruncType {TRUNC_TOP,TRUNC_RIGHT,TRUNC_BOTTOM,TRUNC_LEFT };

但是C ++不允许这样做。上面的四个值是常量和静态的,并且只能由私有类方法使用。


如果你放弃''静态'',它应该做你需要的。如果您在私人部分中放置了枚举声明

,那么除了班级本身及其朋友之外,任何人都无法访问


我可以这样做:

静态const int TRUNC_TOP;
静态const int TRUNC_RIGHT;
等......

将它们初始化为我的不同值。 cpp文件。

但是使用枚举,或者特别适合这个目的的其他东西会更好。我想要做的最好的方法是什么?即,声明四个静态类常量,其值与
无关,除非作为区分标记。
I would like to write, within the private section of my class
definition:

static enum TruncType {TRUNC_TOP,TRUNC_RIGHT,TRUNC_BOTTOM,TRUNC_LEFT};

but C++ does not allow this. The four values above are constant and
static, and will be used only by a private class method.
If you drop ''static'', then it should do what you need. If you place
that enumeration declaration in the private section, it should be
inaccessible to anybody but the class itself and its friends.

I could just do:

static const int TRUNC_TOP;
static const int TRUNC_RIGHT;
etc...

initializing them to distinct values in my .cpp file.

But it would be nicer to use an enum, or somehting else specifically
suited to this purpose. What is the best way to what I''m trying to
do? Ie, declare four static class constants whose values are
irrelevant except as distinguishing markers.




您可能会混淆枚举器和静态数据。调查员

不是静态数据。它们被命名为常量。


Victor



You''re probably confusing enumerators and static data. Enumerators
are not static data. They are named constants.

Victor


你可能会混淆枚举器和静态数据。调查员
不是静态数据。它们被命名为常量。
You''re probably confusing enumerators and static data. Enumerators
are not static data. They are named constants.




对。但在这种情况下,我希望我的命名常量是静态的。

每个实例都没有理由拥有它们自己的副本。

除此之外,枚举是完美的。所以决定是

之间:


1.使用静态命名常量(语法庞大,但节省内存)


2.使用枚举(更清晰的语法,但不必要的重复)


你怎么看?


谢谢,

cpp



Right. But in this case I would like my named constants to be static.
There''s no reason for each instance to have its own copy of them.
Other than that, an enumeration is perfect. So the decision is
between:

1. Use static named constants (bulkier syntax, but memory savings)

2. Use enum (cleaner syntax, but unnecessary duplicates)

What do you think?

Thanks,
cpp


cppaddict写道:
cppaddict wrote:
你是可能会混淆枚举器和静态数据。调查员
不是静态数据。它们被命名为常量。
You''re probably confusing enumerators and static data. Enumerators
are not static data. They are named constants.



对。但在这种情况下,我希望我的命名常量是静态的。
每个实例都没有理由拥有它们自己的副本。
除此之外,枚举是完美的。所以决定之间的关系是:

1.使用静态命名常量(语法庞大,但节省内存)

2.使用枚举(更清晰的语法,但不必要重复)



Right. But in this case I would like my named constants to be static.
There''s no reason for each instance to have its own copy of them.
Other than that, an enumeration is perfect. So the decision is
between:

1. Use static named constants (bulkier syntax, but memory savings)

2. Use enum (cleaner syntax, but unnecessary duplicates)




我的理解是enum声明声明* types * not * variables *。

类型声明不应该有内存。

最好


Kai-Uwe Bux



My understanding is that enum declarations declare *types* not *variables*.
No memory should be unsed for a type declaration.
Best

Kai-Uwe Bux


这篇关于静态枚举替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆