【问题标题】:Arduino equivalent to VB WITHArduino相当于VB WITH
【发布时间】:2018-01-30 22:18:24
【问题描述】:

是否有与 Visual BASIC “with”概念等效的 Arduino?

如果我有和 Arduino 结构如下:

typedef struct {
  int present = 0; // position now
  int demand = 0;  // required position
} superStruct;

superStruct super;

我可以说

if (super.present > super.demand) { super.present-=1; }

有什么方法可以把它缩短到

with super {
  if (.present > .demand) { .present-=1; }
}

谢谢!

【问题讨论】:

  • 您有什么顾虑?减少打字,还是别的什么?
  • 易于阅读。减少打字是一个额外的好处。事实上,他们在 VB 中发明“WITH”时存在的所有顾虑。
  • 不,没有。
  • 想把它转换成答案吗?

标签: c++ struct typedef


【解决方案1】:

只是为了补充 John Bode 的回答:请注意结构¹的方法 可以访问不带前缀的成员:

struct superStruct {
  int present = 0; // position now
  int demand = 0;  // required position
  void update_position() {
    if (present > demand) { present-=1; }
  }
};

superStruct super;

super.update_position();

¹C++ 中的结构只是一个类,默认情况下所有成员都是公共的。

【讨论】:

  • 我想我通常只希望通过一种方法访问成员。这实际上可能是我所寻求的。
【解决方案2】:

C++ 中没有等价的语法;您必须与成员一起指定 struct 实例。

【讨论】:

    猜你喜欢
    • 2022-08-22
    • 2010-12-11
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2011-03-11
    相关资源
    最近更新 更多