扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:李马编译 来源:VCKBASE 2007年10月19日
关键字:
#include <iostream> using namespace std; class CPoint { public: int m_ix; int m_iy; virtual ~CPoint() { } // 译注:原文此处有分号,我将其去掉,下皆同 }; int main() { CPoint objPoint; cout << "Size of Class = " << sizeof(objPoint) << endl; cout << "Address of Class = " << &objPoint << endl; return 0; } |
Size of Class = 12 Address of Class = 0012FF68 |
#include <iostream> using namespace std; class CPoint { public: int m_ix; int m_iy; CPoint(const int p_ix = 0, const int p_iy = 0) : m_ix(p_ix), m_iy(p_iy) { } int getX() const { return m_ix; } int getY() const { return m_iy; } virtual ~CPoint() { } }; int main() { CPoint objPoint(5, 10); int* pInt = (int*)&objPoint; *(pInt+0) = 100; // 企图改变x的值 *(pInt+1) = 200; // 企图改变y的值 cout << "X = " << objPoint.getX() << endl; cout << "Y = " << objPoint.getY() << endl; return 0; } |
int* pInt = (int*)&objPoint; *(pInt+0) = 100; // 企图改变x的值 *(pInt+1) = 200; // 企图改变y的值 |
X = 200 Y = 10 |
int* pInt = (int*)&objPoint; *(pInt+1) = 100; // 企图改变x的值 *(pInt+2) = 200; // 企图改变y的值 |
#include <iostream> using namespace std; class CPoint { public: int m_ix; int m_iy; CPoint(const int p_ix = 0, const int p_iy = 0) : m_ix(p_ix), m_iy(p_iy) { } int getX() const { return m_ix; } int getY() const { return m_iy; } virtual ~CPoint() { } }; int main() { CPoint objPoint(5, 10); int* pInt = (int*)&objPoint; *(pInt+1) = 100; // 企图改变x的值 *(pInt+2) = 200; // 企图改变y的值 cout << "X = " << objPoint.getX() << endl; cout << "Y = " << objPoint.getY() << endl; return 0; } |
X = 100 Y = 200 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者