struct in class access

js.mdnq js_adddot+mdng at gmail.com
Sat Dec 8 23:39:25 PST 2012


On Sunday, 9 December 2012 at 07:24:57 UTC, Jonathan M Davis 
wrote:
> On Sunday, December 09, 2012 07:54:25 js.mdnq wrote:
>> Why can't a struct inside a class access the members of that
>> class without a this pointer? It would seem natural to me that 
>> a
>> nested struct should probably be special in that it is really
>> just a special container to reduce clutter in the class.
>
> Without a this pointer, there are no class members to access, 
> because they
> have to be associated with a specific instance of the class 
> unless they're
> static. Non-static, nested structs have access to their 
> enclosing scope, but
> then you can't create them separate from the enclosing class. 
> However, if you
> declare a nested class to be static, it has no access to the 
> class' members
> and is not associated with a specific instance of the class. 
> It's just that
> it's declared inside the class instead of outside of it.
>
> - Jonathan M Davis

NOOOOOOOOO!!!! It does have a this pointer!! If the sub-struct is 
a true sub struct then it is inline inside the memory of the 
class! That is, any member inside a struct can easily be gotten 
from the pointer to the class(object) as one just has to add a 
simple(and static) offset.

If the struct is only used inside the class then there should be 
no problem.

If you look at the two examples I gave between a class and a 
struct inside a class, there is NO difference except for syntax! 
(excluding the way D does it already).

I'm not trying to define how D does it, but how D should do 
it(assuming there is no blatant logic errors that makes it 
impossible).

It makes no sense to have a struct inside a class behave exactly 
as that outside as it offers no benefit to do so(or maybe it 
does, but very little). Hence, we can redefine the way structs 
behave inside classes to make them more useful.

In fact, maybe a struct is not the best way to do this but it 
requires very little modification.

In any case, take this example:

class A {
public:
     string Name;
     struct B { public: int x; alias x this; void func(A _a) { 
writeln(_a.Name, x, y); }}
     B x;
     B y;
}

...

A a;

What is the address of A?

What is the address of x inside A? (i.e., the struct inside A?)

Is it not a simple static offset from the address of A? i.e., 
knowing the address of a lets us know the address of x. Knowing 
the address of x also lets us know the address of a! (Same goes 
for y)

This is why a nested struct(using my semantics) contains the this 
pointer! (because it is a simple offset from the this pointer 
which can be computed at compile time)!

NOW!! In this case, a nested struct is simply making an 
encapsulation of class data but effectively is equivalent to 
having the data inside the class. (but allows us to avoid 
collisions between overrides in the class and the struct, which 
is why I think it will be more useful to have such behavior than 
the current(since the current seems to offer nothing useful).



(












More information about the Digitalmars-d-learn mailing list