简体   繁体   English

C ++枚举副本构造函数和赋值运算符

[英]C++ enumeration copy constructor and assignment operator

I just found out that enumerations have default constructors and assignment operators in C++. 我刚刚发现,枚举在C ++中具有默认的构造函数和赋值运算符。 Does anyone have an example of an enumeration with non-defaults copy constructor and assignment operator ? 有没有人有非默认副本构造函数和赋值运算符的枚举示例?

enum are just int s, that's why they will always have "assignment operator". enum只是int ,这就是为什么它们将始终具有“赋值运算符”的原因。

By the standard by default , the first element of an enum always has 0 as value, and all other elements, after the first one, are "previous_value + 1". 默认情况下默认情况下enum的第一个元素的值始终为0 ,而在第一个元素之后的所有其他元素均为“ previous_value + 1”。

You can change the value of the first element, of course. 当然,您可以更改第一个元素的值。 Actually, you can give values for each "member" of the enum. 实际上,您可以为枚举的每个“成员”提供值。
Thanks to @Konrad Rudolph for the comment. 感谢@Konrad Rudolph的评论。 I just didn't mention "by default" at the beginning, as we were talking about "default construction". 一开始我只是没有提到“默认”,因为我们在谈论“默认构造”。

So no, there are no enum s without "assignment operator" and "default/copy constructor". 所以不,没有“赋值运算符”和“默认/复制构造函数”的enum就没有了。

Enumerators behave much like integral types except for fewer implicit conversions. 枚举器的行为与整数类型非常相似,除了隐式转换较少。 I can't think of a case where there would be non-default copy constructor or assignment operators for an enumeration (unless you wrapped it in a class). 我想不出会有非默认的复制构造函数或赋值运算符进行枚举的情况(除非您将其包装在类中)。

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

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