Pass a pointers value by reference
Is it possible to refer to the address value that the pointer variable holds.
For example, my function is something(int &num);
and I need to pass a pointer to it, such as int* i = new int(1); like &(*(i));
Can this be done?
I am using qt and I don't know why but the following occurs...
*&ui->numberInput->text()
gives aQString
&ui->numberInput->text()
gives a*QString
*ui->numberOutput->text()
gives*QLabel-text()
ui->numberInput->text()
gives aQString
It wont be accepted as 开发者_StackOverflow社区a &QString
?
Could someone please help me with this problem
you simply pass it like this
something(*i);
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.
Yes. The syntax is *&p.
精彩评论