0

I would like to know the difference between defining a function to take an argument to a reference of an object (case 1) and defining a function to take an argument to a pointer of an object (case 2).

Of course the way that you refer to the object itself differs, therefore I am asking about the different capabilities that one has by using the one or the other way when defining a function, as well as the context that the one or the other way of defining a function is used.

1) Reference to the object in the function's arguments

void function(T& t) { 

  // Procedure that might change 
  // the value of the object t.

  // t is the object

}

void main() {

  T object;
  function(object);

}

2) Pointer to the object in the function's arguments

void function(T* t) { 

  // procedure that might change 
  // the value of the object *t 

  // *t is the object
}

void main() {

  T object;
  function(&object);

}

Note: there are some questions that discuss the difference between a pointer variable and a pointer variable, but this one examines the context of using the as function arguments.

2
  • 2
    You might also be interested in difference between a pointer and reference parameter? Commented Oct 10, 2015 at 22:50
  • They are basically the same except where they are different... Pointers can be NULL, references are not supposed to be. The parameters are treated differently when resolving overloaded functions. Commented Oct 10, 2015 at 22:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.