Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
超载++;作用域枚举类类型的运算符 我一直在实验C++的EnUM类特性,并成功地获得了++操作符的超载: enum class counter_t : uint8_t {VAL1 = 0, VAL2, VAL3, VAL4, END}; inline counter_t operator ++ (counter_t c, int) { counter_t c2; if (c == counter_t::END) { c2 = counter_t::VAL1; } else { c2 = (counter_t)((uint8_t)c + 1); } return (c2); } int main(void) { volatile counter_t x = counter_t::VAL1; x = x++; x++; while(1) { //do stuff } }_C++_Enums_Operator Overloading - Fatal编程技术网

超载++;作用域枚举类类型的运算符 我一直在实验C++的EnUM类特性,并成功地获得了++操作符的超载: enum class counter_t : uint8_t {VAL1 = 0, VAL2, VAL3, VAL4, END}; inline counter_t operator ++ (counter_t c, int) { counter_t c2; if (c == counter_t::END) { c2 = counter_t::VAL1; } else { c2 = (counter_t)((uint8_t)c + 1); } return (c2); } int main(void) { volatile counter_t x = counter_t::VAL1; x = x++; x++; while(1) { //do stuff } }

超载++;作用域枚举类类型的运算符 我一直在实验C++的EnUM类特性,并成功地获得了++操作符的超载: enum class counter_t : uint8_t {VAL1 = 0, VAL2, VAL3, VAL4, END}; inline counter_t operator ++ (counter_t c, int) { counter_t c2; if (c == counter_t::END) { c2 = counter_t::VAL1; } else { c2 = (counter_t)((uint8_t)c + 1); } return (c2); } int main(void) { volatile counter_t x = counter_t::VAL1; x = x++; x++; while(1) { //do stuff } },c++,enums,operator-overloading,C++,Enums,Operator Overloading,相当直截了当。“x=x++;”行可以正常工作,但“x++;”行不行。对于autoincrement版本,++运算符函数的正确形式是什么?仅在出现错误之后,以下代码就可以在MSVC上编译并运行良好。注意函数参数中的易失性和&。还有c2=c和一些修改,以遵循++标准(返回值,然后递增)volatile是必需的,因为您将x声明为volatile inline counter_t operator ++ (volatile counter_t &c, int) { counter_t

相当直截了当。“x=x++;”行可以正常工作,但“x++;”行不行。对于autoincrement版本,++运算符函数的正确形式是什么?

仅在出现错误之后,以下代码就可以在MSVC上编译并运行良好。注意函数参数中的
易失性
&
。还有
c2=c
和一些修改,以遵循++标准(返回值,然后递增)
volatile
是必需的,因为您将x声明为
volatile

inline counter_t operator ++ (volatile counter_t &c, int) 
{
    counter_t c2;

    if (c == counter_t::END)
        c2 = counter_t::VAL1;
    else
        c2 = static_cast<counter_t>(static_cast<uint8_t>(c) + 1);

    c = c2;

    return c2;
}
inline counter_t operator++(volatile counter_t&c,int)
{
计数器c2;
if(c==计数器\u t::END)
c2=计数器\u t::VAL1;
其他的
c2=静态施法(静态施法(c)+1);
c=c2;
返回c2;
}

您可以使用它来实现前缀增量:

inline counter_t& operator ++ (counter_t& c) {
  if (c == counter_t::END)
      c = counter_t::VAL1;
  else
      c = counter_t(unsigned(c) + 1);
  return c;
}
inline counter_t operator ++ (counter_t& c, int) {
  counter_t result = c;
  ++c;
  return result;
}
现在,您可以使用前缀增量来实现后缀增量:

inline counter_t& operator ++ (counter_t& c) {
  if (c == counter_t::END)
      c = counter_t::VAL1;
  else
      c = counter_t(unsigned(c) + 1);
  return c;
}
inline counter_t operator ++ (counter_t& c, int) {
  counter_t result = c;
  ++c;
  return result;
}
测试:

#包括
内部主(空){
计数器\u t前缀=计数器\u t::VAL1;
for(无符号i=0;i<5;++i)

std::不能通过引用获取参数
计数器c
并修改itUmm…
++
运算符只接受一个操作数,不是吗?为什么有
计数器t
int
?@Jashaszun这取决于,前缀增量不接受额外的参数,但后缀运算符接受额外的伪
int
argument,因此它有一个不同于前缀运算符的函数签名。
x=x++;
是否仍然未定义,即使
operator++
重载?@jxh-Nope,写入
x=operator++(x,0)是有效的函数调用比内置运算符具有更多的顺序限制。您没有遵循
x++
的标准。它应该是
{auto old=c;++c;return old;}
并将函数用于
++x
。其中的volatile用于防止编译器将程序优化为零(即使在-O1)。我将其拒绝为无优化,并且不需要volatile。嘿,这很有效。我认为pass-by-reference应该在那里的某个地方,但不知道static\u-cast。非常感谢。
static\u-cast
在这种情况下并不重要。我困扰我,因为我在VS中用下划线作为警告“C样式转换而不是C++”。请尽量避免C样式转换,它们有时可能会有问题。