如何能访问一个对象的vptr啊

szlbyou 2002-02-02 12:59:53
例:
class test{
public:
test(){...}
virtual ~test(){...}
private:
...
};


如何能访问一个对象的vptr,能访问吗?
...全文
66 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LLnju 2002-02-02
  • 打赏
  • 举报
回复
下面的测试在 VC上通过:
#include <windows.h>
#include <iostream>
#include <vector>
using namespace std;

struct testVtbl
{
int a , b;
virtual void __cdecl show() const
{
cout << a << " " << b << endl;
}
virtual void __cdecl showB() const
{
cout << b << " " << a << endl;
}
virtual void __cdecl showInfo( const char* inf ) const
{
cout << inf << endl;
}
};

typedef void (__cdecl * pFun )( const testVtbl* pThis );
typedef void (__cdecl * pFun2 )( const testVtbl* pThis , const char* inf );

void main()
{
testVtbl tv;
testVtbl* ptv = &tv;
tv.a = 100; tv.b = 10000;
pFun* __pvfptr = reinterpret_cast<pFun**>( ptv )[0];
pFun pp = __pvfptr[0];
pp( ptv );
pp = __pvfptr[1];
pp( ptv );

pFun2 pp2 = reinterpret_cast<pFun2*>(__pvfptr)[2];
pp2( ptv , "this is a test" );
}
hz129 2002-02-02
  • 打赏
  • 举报
回复
只要知道它的存在就行了,何必要访问它?

每个编译器中它所存放的位置都可能不一样,即使访问了也没有移植性可言,根本毫无意义的
liu_feng_fly 2002-02-02
  • 打赏
  • 举报
回复
有必要访问他吗?何必研究这种无聊问题呢
szlbyou 2002-02-02
  • 打赏
  • 举报
回复
请各位赐教!

69,378

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧