-4
我正在研究訪問私人班級成員。我想更好地瞭解這一點。'&'表示堆分配指針是什麼意思?
class Sharp
{
public:
Sharp();
~Sharp();
private:
DWORD dwSharp;
public:
void SetSharp(DWORD sharp) { dwSharp = sharp; };
};
Sharp::Sharp()
{
dwSharp = 5;
}
Sharp::~Sharp()
{
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD a = 1;
*(DWORD*)&a = 3;
Sharp *pSharp = new Sharp;
cout << *(DWORD*)&pSharp[0] << endl;
cout << *(DWORD*)pSharp << endl;
cout << (DWORD*&)pSharp[0] << endl;
//pSharp = points to first object on class
//&pSharp = address where pointer is stored
//&pSharp[0] = same as pSharp
//I Would like you to correct me on these statements, thanks!
delete pSharp;
system("PAUSE");
return 0;
}
所以我的問題是,什麼是pSharp
,&pSharp
和&pSharp[0]
,也請解釋cout << (DWORD*&)pSharp[0] << endl;
,爲什麼它輸出0000005
。
謝謝!
雖然不是常規的,但我懷疑它幾乎等同於'(DWORD *)&pSharp [0]',它是'pSharp'的第0個元素的地址作爲指向'DWORD'的指針。 –
'cout <<(DWORD *&)pSharp [0] << endl;' 這是獲得存儲在索引爲'0'的數組中的值,並將其解釋爲指向DWORD的指針。這就是爲什麼結果是'5'。我猜你的意思是: 'cout <<(DWORD *)&pSharp [0] << endl;' –