Will
Will

Reputation: 1

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...

It wont be accepted as a &QString?

Could someone please help me with this problem

Upvotes: 0

Views: 1709

Answers (3)

TheUndeadFish
TheUndeadFish

Reputation: 8171

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.

Upvotes: 1

Axel Gneiting
Axel Gneiting

Reputation: 5393

Yes. The syntax is *&p.

Upvotes: -1

Vinzenz
Vinzenz

Reputation: 2819

you simply pass it like this

something(*i);

Upvotes: 4

Related Questions