struct in class feature

js.mdnq js_adddot+mdng at gmail.com
Tue Dec 11 22:54:27 PST 2012


On Wednesday, 12 December 2012 at 01:36:55 UTC, Rob T wrote:
> On Tuesday, 11 December 2012 at 03:28:00 UTC, d coder wrote:
>> Greetings
>>
>> I too find this very useful. I am also having this situation 
>> often when
>> there is an array of nested struct instances inside a class 
>> environment.
>> This is a common scenario and deserves attention. Also I 
>> believe (looking
>> at D1 forums) that there was a time in the past when D1 nested 
>> structs did
>> support 'outer'.
>>
>> But unfortunately structs are neglected part of D, so I am not 
>> sure if
>> developers give an ear to this thread. To make D truly a 
>> systems language
>> we need to make structs more robust and feature rich.
>>
>> Regards
>> - Puneet
>
> If I want to manually pass the parent pointer to a child 
> struct, how do I do it? The compiler won't allow me to use 
> "this" from the parent until after the parent has been 
> instantiated.
>
> The solution I came up with is to create a default constructor 
> for the parent that sets the child struct with the parents 
> pointer. It works but maybe there's a better way?
>
> BTW, the same situation happens with structs defined inside 
> structs, they will not have access to the parent struct members 
> unless you manually pass a pointer into the child struct at 
> some point after the parent struct's address becomes known. You 
> cannot create a default constructor for a struct, so reliably 
> initializing the child struct with the parents pointer is 
> problematic.
>
> --rt

The problem with this method is that it requires an extra ptr in 
the child to the parent class. This is not necessary and a waste 
of space. While it work just fine it's not "efficient". Also, the 
compiler should have no issue accessing parent fields because 
they are a simple compile time const difference.

In some sense, if ptr_s is the this variable for a struct. Then 
ptr_s + x would point to a field in the struct. x is known at 
compile time as it depends on the size of the types in the 
struct. A nested struct would simply use ptr_s - y + z to access 
the elements of the parent. ptr_s - y = ptr_p, which is the this 
ptr to the parent class.

Hence, the only thing that would change is the compiler would 
have to compute where the struct is located in the class, then 
subtract that value from the struct's this ptr to get the this 
ptr of the parent object containing that struct object.

I wrote a post here that does something similar but ultimately 
the compiler can do it all for us and would not waste any space 
or extra cycles.

http://forum.dlang.org/thread/dgleowuonachabwxmlrx@forum.dlang.org

The main thing to realize is that the offsets are all known at 
compile time(obviously) and the relationship between a nested 
struct object and it's parent is also known at compile time. By 
having to "hard code" stuff to get it to work though, makes it 
difficult to manage.


More information about the Digitalmars-d mailing list