如何获取使用void指针而不是对象访问类的成员变量 [英] How to gain Access to member variables of a class using void pointer but Not Object

查看:172
本文介绍了如何获取使用void指针而不是对象访问类的成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问类的成员变量而不使用对象。请让我知道如何去。

  class TestMem 
{
int a;
int b;
public:
TestMem(){}
void TestMem1()
{
a = 10;
b = 20
}
};

void(TestMem :: * pMem)();

int main(int argc,char * argv [])
{

TestMem o1;
pMem =&(TestMem :: TestMem1);

void * p =(void *)& pMem;
//如何访问& b成员变量使用变量p
getch();
return 0;
}


解决方案

这是通过使用 offsetof() macro from < stddef.h> 。很遗憾, offsetof()有一些相当的 C ++中的结构严重限制


由于C ++中struct的扩展功能,在这种语言中,使用 offsetof 被限制为POD [plain old data] types,对于类,或多或少对应于struct的C概念(尽管非派生类只有public-虚拟成员函数,没有构造函数和/或析构函数也将成为POD)。


因此,如果你使 a b public并摆脱 TestMem 的构造函数,你可以这样写访问 a



C ++ style:



  #include< cstddef& 

int vala = * reinterpret_cast< int *>(reinterpret_cast< char *>(& o1)
+ offsetof(TestMem,



C style:



  #include< stddef.h> 

int vala = *(int *)((char *)& o1 + offsetof(TestMem,a));

请注意,您需要使用& o1 这里,不是 p ,这是一个函数指针。 TestMem :: TestMem1 的地址与 a b 。类方法不驻留在类成员变量附近的内存中。






<错误的方式是猜测在 a b 在内存中。很可能它们分别位于 o1 开始的偏移量0和4。所以这段代码在大部分时间都会工作:

  int vala = *(int *)((char *)& o1 + 0); 
int valb = *(int *)((char *)& o1 + 4);

这里有很多假设。这假设int是4字节,并且 a b 之间没有填充。另一方面它没有上面的任何限制: a b 不需要是公共的,你可以有一个构造函数,无论。


I am trying to access member variables of a class without using object. please let me know how to go about.

class TestMem
{
    int a;
    int b;
public:
    TestMem(){}
    void TestMem1()
    {
    	a = 10;
    	b = 20;
    }
};

void (TestMem::*pMem)();

int main(int argc, char* argv[])
{

    TestMem o1;
    pMem = &(TestMem::TestMem1);

    void *p = (void*)&pMem;
    // How to access a & b member variables using variable p
    getch();
    return 0;
}

解决方案

The "right" way to do this is by using the offsetof() macro from <stddef.h>. Unfortunately offsetof() has some fairly draconian restrictions in C++:

Because of the extended functionality of structs in C++, in this language, the use of offsetof is restricted to "POD [plain old data] types", which for classes, more or less corresponds to the C concept of struct (although non-derived classes with only public non-virtual member functions and with no constructor and/or destructor would also qualify as POD).

So if you make a and b public and get rid of TestMem's constructor, you can write something like this to access a:

C++ style:

#include <cstddef>

int vala = *reinterpret_cast<int *>(reinterpret_cast<char *>(&o1)
           + offsetof(TestMem, a));

C style:

#include <stddef.h>

int vala = *(int *) ((char *) &o1 + offsetof(TestMem, a));

Notice that you need to use &o1 here, not p, which is a function pointer. The address of TestMem::TestMem1 won't have any relation to the locations of a and b. Class methods don't reside in memory anywhere near class member variables.


The "wrong" way is to just guess at where a and b are in memory. Most likely they are at offsets 0 and 4 from the start of o1, respectively. So this code would work most of the time:

int vala = *(int *) ((char *) &o1 + 0);
int valb = *(int *) ((char *) &o1 + 4);

There are a lot of assumptions here. This assumes that ints are 4 bytes and that there's no padding between a and b. On the other hand it doesn't have any of the restrictions from above: a and b don't need to be public, you can have a constructor, whatever.

这篇关于如何获取使用void指针而不是对象访问类的成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆