2016-10-10 19 views
0

好日子所有QT顯示內存值

我與指針和引用熟悉自己,

道歉,如果這個問題,則爲比需要更多的信息,但它可能是幫助他人

目的:

我傳遞的test_string內存地址爲&test_string一個構造函數,構造函數定義接受的內存添加使用QString *ptr_test作爲參數,並在新類中將其指定給由QString *pointer = ptr_test定義的指針位置。

這實質上是一個傳遞引用方法,但只是一直使用內存地址。

問題:

現在我在pointer存儲器地址,我可以使用什麼方法在地址顯示值和/或分配給這例如QString pointerValue其中pointerValue將包含在pointer

指定的信息的所述存儲器地址的值:

長途區號

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(); 

部首

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; 

實施

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; 

輸出

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 
+1

你有什麼期望的價值是什麼?指針地址可能是一些「神祕」的東西,它們會記住對象。 – Hayt

+0

但是,在這裏你有一些關於指針的詳細信息以及如何使用它們:http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean – Hayt

+1

'QString'本身不包含值。所以目前還不清楚你想做什麼。 'QString'使用裏面的'QSharedData'來保留文本。它實現了COW,所以保持原始指向內部文本是不安全的。 –

回答

0

我發現我的解決方案(如果有一個會叫的話)。

我需要申請一個名爲由@Hayt給出

dereferencing鏈接技術 - 看到雖然單詞「的含義/聲音」並沒有關聯到其實際作用的問題

評論。查看提供的鏈接。

起初,我有以下

QString *pointerValue = ptr_test; 
qDebug() << "Display *pointerValue = ptr_test : " << pointerValue; 

    //Output : Display pointerValue = ptr_test : 0x7fffffffd330 <--- the value contained by '*pointer' which is 0x7fffffffd330 
    //note that at address '0x7fffffffd330', the value contained there is the string "This string is created in loginwindow and pointer sent to dialog" 

qDebug() << "Display &pointerValue = ptr_test : " << &pointerValue; 

    //Display &pointerValue = ptr_test : 0x7fffffffd270   <---the address of '*pointer' 

但由於這post我碰到,爲方便閱讀的目的,我引用的部分是幫助我解決這個問題:

下一個示例演示指針的正確用法:

#include using namespace std;

void main() 
{ 
    int x; 
    int *ptr_p; 

    x = 5; 
    ptr_p = &x; 

    cout << *ptr_p;   < --- note the * to refer to the memory location: added by @KGCybeX 
} 

注意:如果你忘記將*(在指針的前面)在COUT語句,你將打印整數x的地址。 (嘗試一下)。

的解決辦法/回答

因此我的顯示解決方案包含另一個變量的內存地址的指針:

qDebug() << "Display *pointerValue = ptr_test : " << *pointerValue; 
//Display *pointerValue = ptr_test : "This string is created in loginwindow and pointer sent to dialog"