How to get the address of an heir?

Welcome!
There is a class MyClass inherited from the abstract class IInterface
MyClass * myClass = new MyClass(0);
IInterface * interface = myClass;

In the kitchen, I have only the interface address, nothing more! ie let zagolovok MyClass to change the code or IInterface, MyClass I can't.
How to obtain from him the address of myClass ?
If I do c:
qDebug()<<"MyClass"<<myclass; qdebug()<<"interface"<<interface;< code></myclass;>
<br / > of course the addresses are different... <br> need to somehow through the interface to obtain the address of myClass.<br> You can move the pointer, for example 2 and to the right address, but it is a crutch.<br> PS Yes this is really necessary, but sure.<br> P. P. S. code for example. In reality, there is only zagolovok IInterface variable interface pointer to it.
June 8th 19 at 17:13
2 answers
June 8th 19 at 17:15
Solution
Exactly what to do. Because you have to establish a pattern of "Public Morozov" in very difficult conditions (not even a header). So you will have to repeat what you normally do, the compiler and linker.

Can help the device tables of virtual methods and dynamic_cast it in your compiler. For example, on MinGW I did that.
#include <iostream>


class A {
public:
 int a;
 virtual void doA() {}
 virtual ~A() = default;
};

// class B, C, D, E similarly, {

class DE : public D, public E { int de; };
class AB : public A, public B { int ab; };
class C : public C, public DE { int cde; };

class All : public AB, public C {};


struct Vtable {
 uint32_t d[1];
};

union PObject {
 void* asVoid;
 Vtable** asVtable;
 char* asRaw;
};

int main()
{
 All;
 All* pAll = &all;
 E* pThis = &all;
 std::cout << pAll << "" << pThis << std::endl;

 PObject pX;
 pX.asVoid = pThis;
 while (true) {
 uint32_t offset = -(*pX.asVtable)->d[-2];
 std::cout << "Got offset" << offset << std::endl;
 if (offset == 0)
break;
 pX.asRaw -= offset;
 std::cout << "Got profile" << pX.asVoid << std::endl;
}

 return 0;
}</iostream>

To the intermediate descendant (DE or CDE) to reach out failed.
Given the fact that this is supposed to be compiled with on different platforms this is the full ve...
Apparently still have and see how it behaves in different places...
It is a pity that the class is no longer what is not inherited, it would be possible to cast to obscene so to speak denominator... and I would need the address... - darrell commented on June 8th 19 at 17:18
You should tag Qt, and I guess for what it is: for Qt'shnyh pimpl's. Open Qt again, maybe collect some sort of header from the Qt source code? With a strong desire — even automatically, with the next library update. - Humberto_Lynch68 commented on June 8th 19 at 17:21
Well, almost =) I'm trying the behavior of Q_PROPERTY and the meta to change in their own way without interference in Qt.
While I was staying on pointer arithmetic and a custom to void*. All that were the compilers and platforms tested, like OK, good case assert s metal. - darrell commented on June 8th 19 at 17:24
June 8th 19 at 17:17
Solution
because it is unknown how the account will have a vtable IInterface, MyClass,
usually layout such vtable1/class data...vtablen/class data. Read more I advise you to read
https://shaharmike.com/cpp/vtable-part2/

Find more questions by tags C++Qt