so true

心怀未来,开创未来!
随笔 - 160, 文章 - 0, 评论 - 40, 引用 - 0
数据加载中……

C++中突破private的方法

突破private的方法:
=====================================
对于private变量:
方法一:
class Y
{
public:
 Y(int y=0):m_n(y){};
 int getInt(){return m_n;}
private:
 int m_n;
};

Y y(34);
cout<<y.getInt()<<endl;
int* pi=(int*)(&y);
*pi=347;
cout<<y.getInt()<<endl;
方法二:
int tmp;
__asm
{
 mov eax,y.m_n
 mov tmp,eax
 add y.m_n,5
}
cout<<tmp<<' '<<y.getInt()<<endl;
========================================
对于private函数:
class C1
{
public:
 virtual void test()
 {
  cout<<"public C1::test()"<<endl;
 }
};

class C2: public C1
{
private:
 virtual void test()
 {
  cout<<"private C2::test()"<<endl;
 }
};

C2 c2;
C1 * pC1=&c2;
pC1->test();
方法二:
class O;
typedef void (O::*pO)();

class O
{
public:
 static pO getFunTest()
 {
  return &O::test;
 }
private:
 void test()
 {
  cout<<"this is a private function of class O:test()"<<endl;
 }
};
pO pp=O::getFunTest();
O o;
(o.*pp)();

posted on 2008-07-10 15:58 so true 阅读(424) 评论(0)  编辑  收藏 所属分类: C&C++


只有注册用户登录后才能发表评论。


网站导航: