Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++, when do you need/have to pass data as (*&)

Tags:

c++

I came across people passing data objects as:

declaration:

DataObject * data = 0;

calling it as:

SomeMethod( data );

definition of Somethod:

void SomeMethod(SomeObject * & object)

My obvious question is, when and why do you have to do this (& *)? Is it passing the pointer as reference?

like image 280
ra170 Avatar asked Dec 08 '22 03:12

ra170


1 Answers

Is it passing the pointer as reference?

Yes, that's exactly what it is doing.

This is useful if you want to modify the pointer itself, rather than the data it is pointing to. Remember that C++ passes by value, so if you pass SomeObject*, you are passing a copy of the pointer to SomeObject.

like image 123
Justin Ardini Avatar answered Dec 09 '22 18:12

Justin Ardini