2011-11-12 109 views
-1

我有一個函數,它有兩個參數,兩個對象。我在函數內部更改這些對象,然後我需要看到更改。但指針不起作用。任何想法?更改函數內的對象

void foo(apple &a,apple &b) 
{ 
    //change a and b 
} 
main() 
{ 
    apple a,b; 
    foo(a,b); 
    //a and b are the same as befor calling foo ` 
} 

謝謝。

+0

你是如何「改變」這個對象內的方法? – madth3

+6

請發佈一些*實際*編譯代碼,證明問題。傳遞對象的原則應該是正確的。 –

+0

和指針也應該很好地工作 –

回答

1

你的意思是改變你傳遞的類的方法嗎?如果這就是你的意思,你需要使用' - >'。

class apple { 
    public: 
     int weight; 
} 

void foo(apple *a,apple *b) { 
    a->weight = b->weight; 
} 

main() { 
    apple a,b; 
    foo(&a,&b); 
} 
+0

非常感謝。有效。 – Pegah