I'm studying about accessing private class members. I would like to understand better about this.
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;
}
So my question is, what is pSharp
,&pSharp
and &pSharp[0]
, also please explain cout << (DWORD*&)pSharp[0] << endl;
and why it outputs 0000005
.
Thank you!
what is pSharp
It's a pointer to a Sharp
object instance.
what is &pSharp
It's the address of operator (it doesn't matter that this is a pointer).
what is &pSharp[0]
I don't know why it's written this way but it's just taking the address and the [0] just starts from the beginning of the memory addressed by the pointer.
why it outputs 0000005
Because the dwSharp
class member is set to 5 in the constructor.