std.reflection prototype
bitwise via Digitalmars-d
digitalmars-d at puremagic.com
Wed Apr 15 16:32:09 PDT 2015
On Wed, 15 Apr 2015 15:48:28 -0400, Jacob Carlborg <doob at me.com> wrote:
> On 2015-04-15 17:26, bitwise wrote:
>
>> Right now, this is the def:
>>
>> /**
>> * Array of pairs giving the offset and type information for each
>> * member in an aggregate.
>> */
>> struct OffsetTypeInfo
>> {
>> size_t offset; /// Offset of member from start of object
>> TypeInfo ti; /// TypeInfo for this member
>> }
>>
>> If "string name" esd added, and then offTi[] esd actually populated,
>> then I suppose you could do this:
>>
>> class Test {
>> int a = 4;
>> private int b = 5;
>> void print(){ writeln(b); }
>> }
>>
>> void main()
>> {
>> Test test = new Test;
>> // offsetof would instead come from the TypeInfo/OffsetTypeInfo
>> int* b = cast(int*)(cast(void*)test + Test.b.offsetof);
>> *b = 1234;
>> test.print();
>> }
>>
>> But AFAIK, this is NOT ok in C++ because of the way inheritance works..
>> is this safe in D?
>
> I'm not sure, I would assume so. Why is this not safe in C++?
>
One reason is that casting with multiple inheritance offsets the pointer,
and I forget exactly virtual inheritance works, but I'm sure it breaks
things too..
#include <iostream>
using namespace std;
class A {
public: int a;
};
class B {
public: int b;
};
class C {
public: int c;
};
class D : public A, public B, public C {
public: int d;
};
int main(int argc, const char * argv[])
{
D *d = new D;
cout << (intptr_t)d << endl;
cout << (intptr_t)(C*)d << endl;
cout << (intptr_t)(B*)d << endl;
cout << (intptr_t)(A*)d << endl;
return 0;
}
possible output:
1098416
1098424
1098420
1098416
More information about the Digitalmars-d
mailing list