PDA

View Full Version : Why do we use "&"?



SWEngineer
25th April 2011, 11:15
At the following web page: http://web.mit.edu/qt-dynamic/www/tutorials-tutorial-t2.html

In:


QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));

Why is & used with quit and app? Cannot we use quit and app without &?

Thanks.

SixDegrees
25th April 2011, 11:33
connect expects a pointer.

john_god
25th April 2011, 11:52
"&" refers to the memory adress of the variable quit

DanH
26th April 2011, 12:45
"&" (in this context) means "take the address of what follows". In the example you quote, it's being used because "quit" and "app" are objects, and "connect" simply wants pointers to objects.

In other cases the construct might be used on a call so that a paramater could be modified by the callee. Eg:

int width;
int height;
height = getEiffelTowerDimensions(&width);
....
int getEiffelTowerDimensions(int* width) {
*width = 100;
return 300;
}