Menu
Home
Create new Paste
Log in
Nested struct in class
Code
Theme: cobalt
Theme: eclipse
Theme: elegant
Theme: monokai
Theme: neat
Theme: night
Theme: rubyblue
module main; import std.stdio; class A { struct B(int ofs) { int Value; A Parent() { auto p = cast(void *)&this - ofs; return cast(A)(p); } } B!(8 + 0) b1; // 8 + 0 = offset of b1 into class B!(8 + 4) b2; // 8 + 4 = offset of b2 into class string Name; this() { Name = "Class"; } } int main(string[] argv) { auto asize = A.classinfo.init.length; auto bsize = A.B!(0).sizeof; A a = new A(); auto x = a.b1.Parent(); auto y = a.b2.Parent(); auto a_ptr = cast(void*)a; auto x_ptr = cast(void*)x; auto y_ptr = cast(void*)y; assert(a_ptr == x_ptr); assert(x_ptr == y_ptr); getchar(); return 0; }
Result:
Success
/
Return code: 0
/
Compilation time:
0.602
seconds
/
Run time:
0.084
seconds
Disassembly
Username
Message
Add comment
Paste info
Author:
Guest
Views:
119
Private:
no
Expires:
Never
Uploaded:
12.12.12 8:38
Votes
:
0
Tweet
Compilation
Compiler:
DMD 2.060
Pointer size:
m32
Actions
Download
Fork
Raw
×
Confirm
Are you sure you want to delete this paste?
There's no way back!
×
Confirm
Reason