0

Possible Duplicate:
Difference between pointer to a reference and reference to a pointer

Hi, I could not understand get(Event*& pEvent)... Is it passing the address of the object pEvent to the reference of pEvent..(Event*& pEvent)...The reference will exist as a local scope within the function isnt it....

void classA::func()
{
 Event* pEvent = NULL;
  if ( get((pEvent) )
  {
   ......//definition;
  }
}
bool ClassA::get(Event*& pEvent)
{
 ...//definition;
}
Community
  • 1
  • 1
Angus
  • 12,133
  • 29
  • 96
  • 151

2 Answers2

0

foo(someType& o) means that a reference of an object is passed rather than a copy of the object. In your case a reference to an Event pointer is passed. Basically that means that changes inside the function will make changes to the variable being passed visible to the caller of the function. E.g.

void get(Event* e)
{
   e = null;
}

void get2(Event*& e)
{
   e = null;
}

If you call them:

Event *e = new Event();
get(e);
// e has not changed
get2(e);
// e is null now
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
0

get(Event*& pEvent) means a pointer pEvent is being received by reference. So now the pointer itself is modifiable inside the function.

p = &obj;  // p points to obj
get(p);  // p = pEvent is modified to 0
// now outside p points to '0'

It it was getEvent(Event* pEvent), then you can not modify the pointer itself. So whatever you assign to pEvent inside get(), outside it won't be modified.

iammilind
  • 68,093
  • 33
  • 169
  • 336