views:

92

answers:

4

Is it possible to refer to the address value the pointer variable holds. My function is something(int &num); and I need to pass a pointer to it; int* i = new int(1); like &(*(i)); can this be done?

im using qt and I don't know why but; *&ui->numberInput->text() gives a QString &ui->numberInput->text() gives a *QString, *ui->numberOutput->text() gives *QLabel-text() and ui->numberInput->text() gives a QString. It wont be accepted as a &QString??

+3  A: 

you simply pass it like this

something(*i);
Vinzenz
Exactly. You don't worry about whether it's a pass by reference or pass by value, the compiler handles that for you - the syntax from the caller's point of view is the same.
Mark Ransom
+1  A: 

Yes, the value of a pointer can be passed by value, reference and constant reference.

By Value:

void myfunc(char * p_text);

By Reference:

void yourfunc(char *& p_text);

By Constant Reference:

void theirfunc(char * & const p_text);
Thomas Matthews
`void theirfunc(char *
Vinzenz
This doesn't really address the real question, which was how to pass the pointed-to value to a function that expects to receive its parameter by reference. It's not about how to pass pointers by reference. It's confusing because Will uses the phrase "value of the pointer" to refer to the value the pointer *points to*, not the address value the pointer variable *holds*.
Rob Kennedy
Will
A: 

Yes. The syntax is *&p.

Axel Gneiting
It's worthwhile to read the question, not just the title.
Andy Thomas-Cramer
A: 

It wont be accepted as a &QString??

& serves a few different purposes in C++. In the declaration of a type it means "reference" while in usage with a variable it could be "address of". It's not entirely clear which you're trying to indicate here. Showing us what you're trying to pass and then the declaration of the function you're trying to pass it to might have been clearer.

That said, you mention QLabel::text() which returns a QString. If you want to pass the result of text() to a function taking a reference to a QString then you can just do so directly.

// Given a function with this declaration
void SomeFunction(const QString& parameter);
// and also this varable.
QLabel* l = new QLabel;

// Then the following call would work:
SomeFunction(l->text());

On the other hand, if this wasn't what you meant then show us the actual code you're having problems with and the error message that you're getting from it.

TheUndeadFish