would you help me. I'm passing self to some pure virtual purpose of itself.

n->dataCallback(handler, n, hangup);

where n is my class pointer and dataCallback is its very own (pure) virtual function (Yes, it will not make any sense, I simply question why it takes place things i describe below).

now within the declaration:

void MyClass::dataCallback(int handler, void *cbData, bool hangup) {
    MyClass *This = (MyClass*) cbData;
 ....
}

Now after i match it up (the varaiable above) for this (the pointer from the object) at runtime I recieve:

  1. The values from the poiners (the addresses from the object) will vary.
  2. When checking a memeber varaible (This->member) I've found different values inside them. (The right value is within this, whereas This consists of an undefined (in my experience random) value. Note, that around the caller side the worthiness is obviously correct therefore it appears to like magic change throughout the phone call itself, and there's not one other code among.

How on the planet is possible? Any Idea?

I have spent 2 hrs meditation about this, also requested other developers without any results.

For those who have multiple inheritance, casting a pointer to each one of the parent classes may alter the pointer address. Think about the following:

class A
{
public:
    int a;
};

class B
{
public:
    int b;
};

class C : public A, B
{
};

The interior layout of sophistication C will contain two integers a and b. Should you cast a pointer from C* to A* get up. Should you cast it to B*, the pointer will have to be modified since the first aspect in a B should be the integer b!

Should you perform a cast where both types are recognized to the compiler, it is going to do the modifying in ways that is invisible for you - it simply works. The compiler knows exactly what the proper adjustment ought to be. Whenever you cast to void* this mechanism will get damaged.

We do not possess a full testcase to utilize, but when MyClass is polymorphic and related classes exist, then casting can transform the pointer value. Using void* might be breaking that.

This could happen if n is really a pointer to some class based on the one which implements dataCallback, which class has multiple base classes. Just one (non-empty) base object can have a similar address because the complete object. Here's a good example that demonstrates the issue:

#include <iostream>

struct Base {
    int stuff;
    virtual void f(void * p) {
        std::cout << (p == this ? "GOOD\n" : "BAD\n");
    }
};

struct Other {
    int stuff; 
    virtual void g() {}
};

struct Derived : Other, Base {};

int main()
{
    Derived t;
    t.f(&t);     // BAD: Passing pointer to Derived to member of Base
                 // (but not guaranteed to print "BAD" - layout is not specified)

    Base & b = t;
    b.f(&b);     // GOOD: Passing pointer to Base to member of Base
}