July 02, 2004 Re: why cant structs have base structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | On Fri, 02 Jul 2004 16:12:07 +1200, Regan Heath wrote: > struct A { > int aaa; > int bbb; > } > struct B { > A a_instance; > alias a_instance.aaa aaa; > alias a_instance.bbb bbb; > } > > void main() { > B b; > > b.aaa = 5; > b.bbb = 6; > } This works ... #struct A { # int aaa; # int bbb; #} #struct B { # A a_instance; # alias A.aaa aaa; # alias A.bbb bbb; #} # #void main() { # B b; # # b.aaa = 5; # b.bbb = 6; #} But doesn't help if you have two or more instances of A inside B. -- Derek Melbourne, Australia 2/Jul/04 2:19:58 PM |
July 02, 2004 Re: why cant structs have base structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | On Fri, 2 Jul 2004 14:27:17 +1000, Derek Parnell <derek@psych.ward> wrote: > On Fri, 02 Jul 2004 16:12:07 +1200, Regan Heath wrote: > >> struct A { >> int aaa; >> int bbb; >> } >> struct B { >> A a_instance; >> alias a_instance.aaa aaa; >> alias a_instance.bbb bbb; >> } >> >> void main() { >> B b; >> >> b.aaa = 5; >> b.bbb = 6; >> } > > This works ... > > #struct A { > # int aaa; > # int bbb; > #} > #struct B { > # A a_instance; > # alias A.aaa aaa; > # alias A.bbb bbb; > #} > # > #void main() { > # B b; > # > # b.aaa = 5; > # b.bbb = 6; > #} > > But doesn't help if you have two or more instances of A inside B. Thanks.. I still have not got the hang of aliases like this. :) I am trying to emulate having a base struct (like a base class only no virtual functions etc), so there will not be more than one instance of A inside B. This workaround isn't too bad so long as there aren't too many members in the 'base' struct. You can go another level deep but it requires some copy/pasting... struct A { int var1; int var2; } struct B { A a; alias A.var1 var1; alias A.var2 var2; int var3; int var4; } struct C { B b; alias B.var1 var1; alias B.var2 var2; alias B.var3 var3; alias B.var4 var4; } void main() { C c; c.var1 = 5; c.var2 = 6; c.var3 = 7; c.var4 = 8; } Walter: it would be nice if the above could be achieved with struct A { int var1; int var2; } struct B : A { int var3; int var4; } struct C : B { } Much simpler! Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
July 02, 2004 Re: why cant structs have base structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | "Regan Heath" <regan@netwin.co.nz> wrote in message news:opsahbksx25a2sq9@digitalmars.com... > On Thu, 1 Jul 2004 14:12:37 +0200, Ivan Senji <ivan.senji@public.srce.hr> wrote: > > > "Walter" <newshound@digitalmars.com> wrote in message news:cc0hlu$9gv$2@digitaldaemon.com... > >> > >> You can do the following: > >> > >> struct A { ... } > >> > >> struct B > >> { > >> A a; > >> ... more members ... > >> } > >> > > > > Or even (maybe) better: > > > > struct A() { } > > > > struct B > > { > > mixin A!(); > > // ... more members ... > > } > > > > Nope: mixin A!() A is not a template > I am a real idiot for not trying to add member variables. > Besides which you cannot use a mixin to add private members. I tried this and it seems that i can template A() { int x,y; } struct B { private mixin A!(); float a,b; } And i get a nice message "struct B A!().x is private" B b; b.a=3; b.x=4;//<-error here but ofcourse it has to be in another module to manifest this behaviour! > > Regan > > -- > Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
July 02, 2004 Re: why cant structs have base structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | On Fri, 2 Jul 2004 09:06:25 +0200, Ivan Senji <ivan.senji@public.srce.hr> wrote: > "Regan Heath" <regan@netwin.co.nz> wrote in message > news:opsahbksx25a2sq9@digitalmars.com... >> On Thu, 1 Jul 2004 14:12:37 +0200, Ivan Senji <ivan.senji@public.srce.hr> >> wrote: >> >> > "Walter" <newshound@digitalmars.com> wrote in message >> > news:cc0hlu$9gv$2@digitaldaemon.com... >> >> >> >> You can do the following: >> >> >> >> struct A { ... } >> >> >> >> struct B >> >> { >> >> A a; >> >> ... more members ... >> >> } >> >> >> > >> > Or even (maybe) better: >> > >> > struct A() { } >> > >> > struct B >> > { >> > mixin A!(); >> > // ... more members ... >> > } >> > >> >> Nope: mixin A!() A is not a template >> > > I am a real idiot for not trying to add member variables. > >> Besides which you cannot use a mixin to add private members. > > I tried this and it seems that i can > > template A() > { > int x,y; > } > > struct B > { > private mixin A!(); > float a,b; > } > > And i get a nice message "struct B A!().x is private" > B b; > b.a=3; > b.x=4;//<-error here > but ofcourse it has to be in another module to manifest > this behaviour! Yes you can do that, what I actually mean't to say was, you cannot go... template A() { public: void foo() { bar(); } private: int a; } struct B { mixin A; void bar() { a = 5; } } 'int a' is private to mixin A and not accessable at all to B. But, now that I think about it, were I allowed to go... struct A { public: void foo() { bar(); } private: int a; } struct B : A { mixin A; void bar() { a = 5; } } it wouldn't work either as (I assume) A's privates would not be accessable to B (using standard C++ class inheritance etc). So my workaround to make the private section protected is probably the correct way to do it anyway. I'll go put my head in a paper bag now :) (not plastic - cos that's dangerous) Regan. >> >> Regan >> >> -- >> Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ > > -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
July 02, 2004 Re: why cant structs have base structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | "Regan Heath" <regan@netwin.co.nz> wrote in message news:opsaigb8o15a2sq9@digitalmars.com... > On Fri, 2 Jul 2004 09:06:25 +0200, Ivan Senji <ivan.senji@public.srce.hr> wrote: > > > "Regan Heath" <regan@netwin.co.nz> wrote in message news:opsahbksx25a2sq9@digitalmars.com... > ....... > Yes you can do that, what I actually mean't to say was, you cannot go... I finally understand what is troubling you. I read yout previous posts about this but didn't have any time to try it out. > template A() > { > public: > void foo() { > bar(); > } > private: > int a; > } > > struct B > { > mixin A; > void bar() { > a = 5; > } > } > > 'int a' is private to mixin A and not accessable at all to B. But, now that I think about it, were I allowed to go... > > struct A { > public: > void foo() { > bar(); > } > private: > int a; > } > > struct B : A { > mixin A; > void bar() { > a = 5; > } > } > > it wouldn't work either as (I assume) A's privates would not be accessable > to B (using standard C++ class inheritance etc). Yes i also think you are right, mixin has its own scope and decides what to give access to. > So my workaround to make the private section protected is probably the correct way to do it anyway. Conclusion: your workaround isn't a workaround at all, but the way it should work. > > I'll go put my head in a paper bag now :) > (not plastic - cos that's dangerous) :) > > Regan. > > >> > >> Regan > >> > >> -- > >> Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ > > > > > > > > -- > Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
Copyright © 1999-2021 by the D Language Foundation