如何在 vala 中添加对值类型的引用

How can I add a reference to a value-type in vala

在 C++ 中,我可以添加对值类型的引用,例如:

int a = 12;
int &b = a;

a--;
cout << "a = " << a << ", b = " << b << endl;

将给予: a = 11, b = 11

有没有办法在不使用指针的情况下在 vala 中做同样的事情?

Is there a way to do the same in vala

是的。

without using pointers ?

没有

但是,如果您将它们传递给函数,则可以使用 ref 参数:

void decrement (ref value) {
  value--;
}

void do_stuff () {
  int a = 12;
  decrement (ref a);
  assert (a == 11);
}