1
我有一個類有字段,我想調用這個類的方法,並獲得對其中一個字段(不是值!!)的引用。事情是這樣的:返回int參考vala
class Test : Object{
uint8 x;
uint8 y;
uint8 z;
uint8 method(){
if (x == 1){
return y;
}else if (x == 2){
return z;
}
}
public static void main(string[] args){
uint8 i = method(); // get reference to y or z
i++; //this must update y or z
}
}
在C是:
int& method()
{
if (x == 1){
return y;
}else if (x == 2){
return z;
}
}
我怎樣才能在VALA實現這一目標?
編輯:我試圖使用指針,我有以下
public class Test : Object {
private Struct1 stru;
struct Struct1{
uint8 _a;
public uint8 a{
get{ return _a; }
set{ _a = value; }
}
public Struct1(Struct1? copy = null){
if (copy != null){
this._a = copy.a;
}else{
this._a = 0;
}
}
public uint8* get_aa(){
return (uint8*)a;
}
}
public void get_pointer(){
uint8* dst = stru.get_aa();
}
public static int main (string[] args){
Test t = new Test();
return 0;
}
}
但我編譯時得到
/home/angelluis/Documentos/vala/test.vala.c: In function ‘test_struct1_get_aa’:
/home/angelluis/Documentos/vala/test.vala.c:130:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
result = (guint8*) _tmp1_;
^
Compilation succeeded - 2 warning(s)
爲什麼?我正在返回一個uint8 *類型,並試圖將它存儲在uint8 *指針中。
我已更新我的問題。我使用指針,但我在問題中解釋了一些警告。謝謝。 – RdlP
@RdlP:我也更新了我的答案。 –