Both method declarations follow the link?

What is the difference between these two ways of writing a function in C ++? Do they both โ€œfollow the linkโ€? By "pass by reference" I mean that the function has the ability to modify the original object (if there is no other definition, but this is my intention).

From my understanding, when you call f1, you pass the โ€œsynonymโ€ of the original object. When you call f2, you pass a pointer to the object. Does f2 call create a new object *, while f1 does nothing?

f1 (Object& obj) {}
f2 (Object* obj) {}

Thank!

+3
source share
5 answers
f1 (Object& obj) {}

When you call f1, for example f1(o1):

  • obj o1 (), . , o1 obj; ( , &obj==&o1 true)

f2 (Object* obj) {}

f2, f1(&o1):

  • obj &o1; :
 f2(){
    Object* obj=&o1; // just for understanding what happens when you call f2(&o1)
    ...
+1

.

, f2 , , .

+3

, - ++.

+3

++? f1 , f2 . f1 f2 .

f2 *, f1 ? f2 . new, , .

wiki, pst.

+2

All Articles