-3

I always thought that pointers are always passed by reference in c++.

Obj* a=0;

Init(a);

In the implementation of init I initialize the object. But when the function call is finished the pointer a was still null.

So I had to pass the pointer by reference. So the passing was same but I changed the function declaration to

Init(obj*&)

And now it works.

So my questions is what things are by default passed by reference in c++?

Edit: here is the answer that confused me. The guy is passing a char array pointer and that pointer is changed upon return https://stackoverflow.com/a/8032233

bbbbbbbbbb
  • 27
  • 6

1 Answers1

6

No type is always "by default passed by reference in c++". If you want to pass by reference, you have to denote that explicitly in the function signature.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • Not strictly true - for example the default copy constructor has a reference parameter. –  May 23 '19 at 21:04
  • How is the char array changed on return here https://stackoverflow.com/a/8032233 ? – bbbbbbbbbb May 23 '19 at 21:10
  • Yes, that's what I also tried to do in my example. Why it didn't work for me? – bbbbbbbbbb May 23 '19 at 21:11
  • I meant to ask what parameters retain state between function calls? (Without declaring them as a reference in the prototype) – bbbbbbbbbb May 23 '19 at 21:12
  • You seem to have troubles understanding when copying a pointer is okay (the goal is to have access to object pointed by that pointer) and when copying a pointer is not okay (the goal is to chage **that** pointer). – Fureeish May 23 '19 at 21:12
  • In my example I passed a to the int function. Inside the int function I did a new for a but when I came back the pointer was still null. Didn't I just change the object pointed to by a? – bbbbbbbbbb May 23 '19 at 21:15
  • 1
    @bbbbbbbbbb the answer you tagged passes a pointer by value and uses it to change character array. If that is new for you, perhaps you should pickup [a good book](https://stackoverflow.com/a/388282/10147399) – Aykhan Hagverdili May 23 '19 at 21:15
  • @bbbbbbbbbb no, you reassigned a copied pointer, which will have no effect outside of the function's scope. – Fureeish May 23 '19 at 21:16
  • 1
    @bbbbbbbbbb because a copy of the pointer is passed to the function. What you need to understand is, that a pointrr only is a variable itself: you can assign, imitialze, copy it etc. So everytime you pass a pointer into a function you actually copy the pointer. The copied pointers value is still the adress of the memory it points to but the poinzer is another one. You can simply test this by printing the adress of the pointer e.g. `int *p; std::cout << &p << std::endl` outside and inside the function – Yastanub May 23 '19 at 21:16
  • Right. That is what I also thought. I am making a copy. But the answer I linked is also making a copy of char pointer when he is passing it. Right? – bbbbbbbbbb May 23 '19 at 21:19
  • @bbbbbbbbbb i assume your init function does something like `a = new... `? In That case its like every other passed by value variable you only modify the local version. So if you modify the copy of the pointer the actual pointer stays the same except you pass it by reference or as a pointer to a pointer (dont do that in C++). Your example works because not the pointer is changed but the memory it points to. By calling something like `p[1]` you dereference the pointer so you get to the actual memory adress which the actual pointer also points to. – Yastanub May 23 '19 at 21:23