Good day all
I am familiarizing myself with pointers and references,
apologies if the question contians more info than required, but it might be helpful to others
Purpose:
I pass the memory address of test_string
as &test_string
to a constructor, the constructor definition accepts a memory address using QString *ptr_test
as a parameter, and assigning this in the new class to a pointer location defined by QString *pointer = ptr_test
.
This, essentially, is a pass-by-reference method, but just using memory addresses all the way.
Question:
Now I have a memory address in pointer
, what method can I use to display the value at the address and/or assign this to e.g. QString pointerValue
where pointerValue will contain the value of the memory address specified in pointer
For information:
Calling Code
QString test_string = "This string is created in loginwindow and pointer sent to dialog";
qDebug() << "Displaying orignial variable";
qDebug() << "Displaying test_string = " << test_string;
qDebug() << "Displaying &test_string = " << &test_string;
qDebug() << "/nDisplaying tests:";
...
//code inbetween
...
LoginStatusDialog *dlgConnectStatus = new LoginStatusDialog(test_string, test_string, test_string, &test_string);
dlgConnectStatus->exec();
Header
public:
LoginStatusDialog(const QString &_login, const QString _key, const QString _auth_tok, QString *ptr_test, QWidget *parent = 0);
~LoginStatusDialog();
private:
Ui::LoginStatusDialog *ui;
QString login, key, auth_tok, *pointer;
Implementation
LoginStatusDialog::LoginStatusDialog(const QString &_login, const QString _key, const QString _auth_tok, QString *ptr_test, QWidget *parent) :
QDialog(parent), ui(new Ui::LoginStatusDialog), login(_login), key(_key), auth_tok(_auth_tok), pointer(ptr_test)
{
qDebug() << "Passed with const &_login";
qDebug() << "Displaying login = " << login;
qDebug() << "Displaying _login = " << _login;
qDebug() << "Displaying &login = " << &login;
qDebug() << "Displaying &_login = " << &_login;
qDebug() << "\nPassed with const _auth_tok";
qDebug() << "Displaying auth_tok = " << auth_tok;
qDebug() << "Displaying _auth_tok = " << _auth_tok;
qDebug() << "Displaying &auth_tok = " << &auth_tok;
qDebug() << "Displaying &_auth_tok = " << &_auth_tok;
qDebug() << "\nPassed address with const _key";
qDebug() << "Displaying key = " << key;
qDebug() << "Displaying _key = " << _key;
qDebug() << "Displaying &key = " << &key;
qDebug() << "Displaying &_key = " << &_key;
qDebug() << "\nPassed with *ptr_test";
qDebug() << "Displaying test = " << pointer;
qDebug() << "Displaying ptr_test = " << ptr_test;
qDebug() << "Displaying &test = " << &pointer;
qDebug() << "Displaying &ptr_test = " << &ptr_test;
QString *pointerValue = ptr_test;
qDebug() << "Display &pointerValue = ptr_test : " << pointerValue;
Output
Displaying orignial variable
Displaying test_string = "This string is created in loginwindow and pointer sent to dialog"
Displaying &test_string = 0x7fffffffd330
Displaying tests:
Passed with const &_string_
Displaying login = "This string is created in loginwindow and pointer sent to dialog"
Displaying _login = "This string is created in loginwindow and pointer sent to dialog"
Displaying &login = 0x5555559a4550
Displaying &_login = 0x7fffffffd330
Passed with const _string_
Displaying auth_tok = "This string is created in loginwindow and pointer sent to dialog"
Displaying _auth_tok = "This string is created in loginwindow and pointer sent to dialog"
Displaying &auth_tok = 0x5555559a4560
Displaying &_auth_tok = 0x7fffffffd4c0
Passed address with const _string_
Displaying key = "This string is created in loginwindow and pointer sent to dialog"
Displaying _key = "This string is created in loginwindow and pointer sent to dialog"
Displaying &key = 0x5555559a4558
Displaying &_key = 0x7fffffffd4b0
Passed with *ptr_string_
Displaying test = 0x7fffffffd330
Displaying ptr_test = 0x7fffffffd330
Displaying &test = 0x5555559a4568
Displaying &ptr_test = 0x7fffffffcf98
Display &pointerValue = ptr_test : 0x7fffffffd330