简体   繁体   English

指向成员函数调用的指针中的指针“ this”的地址意外更改

[英]Address of pointer “this” changed unexpectedly inside a pointer to member function call

I have problem with the pointer to member function call. 我对成员函数调用的指针有疑问。 The address of pointer "this" outside the function pointer call is different than inside the call thus all access to the class variables results wrong values. 函数指针调用外部的指针“ this”的地址与调用内部的地址不同,因此对类变量的所有访问都会导致错误的值。

I include the code here. 我在这里包括代码。

class ClassInterface
{
public:
    ClassInterface(void);
    ~ClassInterface(void);
};

class ClassA:public ClassInterface
{
public:
    float   _a;
public:
    ClassA(void);
    ~ClassA(void);

    virtual void Update();
};


class ClassB:public ClassA
{
public:
    ClassB(void);
    ~ClassB(void);

    void Update();

    void ProcessTaskB(void*);
};

//ClassB.CPP
void ClassB::ProcessTaskB(void*)
{
    printf("ClassB::ProcessTaskB\n");

    printf("Address of myB INSIDE callback = %d\n",this);
    _a += 100;
}

//test CPP
#include "stdafx.h"
#include "ClassInterface.h"
#include "ClassA.h"
#include "ClassB.h"

typedef void (ClassInterface::*Callback) (void* );

int _tmain(int argc, _TCHAR* argv[])
{
    ClassA* myA = new ClassA();
    ClassB* myB = new ClassB();

    Callback fptrB = (Callback) &(ClassB::ProcessTaskB);

    printf("Address of myB outside callback = %d\n",myB);

    (myB->*fptrB)(NULL);

    return 0;


}

And this is the output: 这是输出:

Address of myB OUTSIDE callback = 1332696
Address of myB INSIDE callback = 1332700

Thus the statement _a += 100; 因此,语句_a + = 100; does not make change to _a. 不会对_a进行更改。 It made change to address (&_a + 4). 它更改了地址(&_a + 4)。

I have no clue to resolve this. 我不知道要解决这个问题。 Please help me fix. 请帮我解决。

In your updated form your code is perfectly correct and has no problems whatsoever. 在更新后的表格中,您的代码是完全正确的,并且没有任何问题。 The only explanation for the incorrect behavior is that you must be using MSVC++ compiler in some "restricted" member pointer model (which works incorrectly in general case). 对于不正确行为的唯一解释是,您必须在某些“受限制的”成员指针模型中使用MSVC ++编译器(在通常情况下,该模型工作不正常)。 In fact, I believe MSVC++ compiler issues the corresponding warnings when you attempt to convert your member pointers. 实际上,我相信当您尝试转换成员指针时,MSVC ++编译器会发出相应的警告。 Did you just ignore the warning? 您只是忽略了警告吗?

Anyway, add 无论如何,添加

#pragma pointers_to_members( full_generality, virtual_inheritance )

to you code in order to enable full functionality of member function pointers required by C++ standard, and your code should work fine. 代码以启用C ++标准所需的成员函数指针的全部功能,并且代码应该可以正常工作。 In your case this 在你的情况下

#pragma pointers_to_members( full_generality, multiple_inheritance )

should be sufficient though. 应该足够了。 Compiler options from /vmm, /vms, /vmv group in combination with /vmg achieve the same effect. /vmm, /vms, /vmv组中的编译器选项与/vmm, /vms, /vmv结合使用/vmg相同的效果。

Also, avoid C-style casts in such contexts. 另外,在这种情况下,请避免使用C样式转换。 The conversion you are using is performed by static_cast 您使用的转换是由static_cast执行的

Callback fptrB = static_cast<Callback>(&ClassB::ProcessTaskB);

Also, don't attempt to print pointers with %d format specifier in printf . 另外,请勿尝试在printf打印具有%d格式说明符的指针。 This is another undefined behavior right there. 这是另一个未定义的行为。 Printing pointers is what %p format specifier is for. 打印指针是%p格式说明符的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM