更改函数内部的对象

change the objects inside functions

本文关键字:对象 内部 函数      更新时间:2023-10-16

我有一个函数,它有两个参数,都是对象。我在函数中改变了这些对象,之后我需要看到这些改变。但是指针不起作用。任何想法?

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  `  
}

谢谢。

你的意思是改变你传递的类的方法吗?如果你是这个意思,你需要使用'->'。

class apple {
    public:
        int weight; 
}
void foo(apple *a,apple *b) {
    a->weight = b->weight;
}
main() {
    apple a,b;
    foo(&a,&b);
}