Original Post
Hello guys,
I know the code below is bad, but i need to know why is this even working. Please read the code.
Question:
1. Why line {int* var = (int*)pDumb;} is working?
2. Why line {pDumb->getX();} shows the output as 10?
3. Why line {pDumb->getY();} shows the output as 0?
4. How can a integer pointer assign value to a private variable?
My environment: Visual Studio Team Edition 2008, Windows XP
Thanks for any explanation.
I know the code below is bad, but i need to know why is this even working. Please read the code.
#include <iostream>class Dumb{ int x; int y;public: const int getX(){ return x; } const int getY(){ return y; }};int main(void){ Dumb* pDumb = new Dumb(); int* var = (int*)pDumb; (*var) = 10; std::cout << "\nDumb::x : " << pDumb->getX(); std::cout << "\nDumb::y : " << pDumb->getY(); delete pDumb; return 0;}Output::Dumb::x : 10Dumb::y : 0Question:
1. Why line {int* var = (int*)pDumb;} is working?
2. Why line {pDumb->getX();} shows the output as 10?
3. Why line {pDumb->getY();} shows the output as 0?
4. How can a integer pointer assign value to a private variable?
My environment: Visual Studio Team Edition 2008, Windows XP
Thanks for any explanation.