digitalmars.D.learn - Struct inside a class: How to get outer?
- Nick Sabalausky (Abscissa) (12/12) Dec 02 2017 Is this even possible? My attempts:
- Jonathan M Davis (6/18) Dec 02 2017 As I understand it, there is no outer for nested structs, only nested
- bauss (4/25) Dec 03 2017 It wouldn't make much sense either, if a struct was able to do
- Nick Sabalausky (Abscissa) (2/9) Dec 03 2017 Why is that?
- Jacob Carlborg (6/11) Dec 03 2017 It would get an extra field, making the size of the struct larger and
- Steven Schveighoffer (10/30) Dec 04 2017 Yes, for structs inside structs or classes, there is no 'outer' member.
- A Guy With a Question (4/39) Dec 04 2017 Yes! Don't forget the 'static' otherwise you'll get some odd
Is this even possible? My attempts: class Outer { struct Inner { void foo() { // Error: no property 'outer' for type 'Inner' Outer o = this.outer; // Error: cannot implicitly convert expression // this of type Inner to testNested.Outer Outer o = this; } } }
Dec 02 2017
On Sunday, December 03, 2017 01:05:00 Nick Sabalausky via Digitalmars-d- learn wrote:Is this even possible? My attempts: class Outer { struct Inner { void foo() { // Error: no property 'outer' for type 'Inner' Outer o = this.outer; // Error: cannot implicitly convert expression // this of type Inner to testNested.Outer Outer o = this; } } }As I understand it, there is no outer for nested structs, only nested classes. So, you'll either have to use a nested class or explicitly pass a reference to the outer class to the nested struct. - Jonathan M Davis
Dec 02 2017
On Sunday, 3 December 2017 at 07:38:47 UTC, Jonathan M Davis wrote:On Sunday, December 03, 2017 01:05:00 Nick Sabalausky via Digitalmars-d- learn wrote:It wouldn't make much sense either, if a struct was able to do it.Is this even possible? My attempts: class Outer { struct Inner { void foo() { // Error: no property 'outer' for type 'Inner' Outer o = this.outer; // Error: cannot implicitly convert expression // this of type Inner to testNested.Outer Outer o = this; } } }As I understand it, there is no outer for nested structs, only nested classes. So, you'll either have to use a nested class or explicitly pass a reference to the outer class to the nested struct. - Jonathan M Davis
Dec 03 2017
On 12/03/2017 03:46 AM, bauss wrote:On Sunday, 3 December 2017 at 07:38:47 UTC, Jonathan M Davis wrote:Why is that?As I understand it, there is no outer for nested structs, only nested classes. So, you'll either have to use a nested class or explicitly pass a reference to the outer class to the nested struct.It wouldn't make much sense either, if a struct was able to do it.
Dec 03 2017
On 2017-12-03 10:57, Nick Sabalausky (Abscissa) wrote:On 12/03/2017 03:46 AM, bauss wrote:It would get an extra field, making the size of the struct larger and not compatible with a C struct. But I guess you wouldn't use a nested struct for that anyway. -- /Jacob CarlborgIt wouldn't make much sense either, if a struct was able to do it.Why is that?
Dec 03 2017
On 12/3/17 2:38 AM, Jonathan M Davis wrote:On Sunday, December 03, 2017 01:05:00 Nick Sabalausky via Digitalmars-d- learn wrote:Yes, for structs inside structs or classes, there is no 'outer' member. However, there is a hidden context member in a struct if it's nested inside a function. In that case, you must label the struct "static" The only reason inner classes have outer pointers to their "owner" class instance, is for those familiar with Java programming style (specifically, IIRC, it was to write dwt, which was a port of jwt). I believe Walter mentioned elsewhere recently, he would have done things differently today. -SteveIs this even possible? My attempts: class Outer { struct Inner { void foo() { // Error: no property 'outer' for type 'Inner' Outer o = this.outer; // Error: cannot implicitly convert expression // this of type Inner to testNested.Outer Outer o = this; } } }As I understand it, there is no outer for nested structs, only nested classes. So, you'll either have to use a nested class or explicitly pass a reference to the outer class to the nested struct.
Dec 04 2017
On Monday, 4 December 2017 at 14:01:08 UTC, Steven Schveighoffer wrote:On 12/3/17 2:38 AM, Jonathan M Davis wrote:Yes! Don't forget the 'static' otherwise you'll get some odd errors.On Sunday, December 03, 2017 01:05:00 Nick Sabalausky via Digitalmars-d- learn wrote:Yes, for structs inside structs or classes, there is no 'outer' member. However, there is a hidden context member in a struct if it's nested inside a function. In that case, you must label the struct "static" The only reason inner classes have outer pointers to their "owner" class instance, is for those familiar with Java programming style (specifically, IIRC, it was to write dwt, which was a port of jwt). I believe Walter mentioned elsewhere recently, he would have done things differently today. -SteveIs this even possible? My attempts: class Outer { struct Inner { void foo() { // Error: no property 'outer' for type 'Inner' Outer o = this.outer; // Error: cannot implicitly convert expression // this of type Inner to testNested.Outer Outer o = this; } } }As I understand it, there is no outer for nested structs, only nested classes. So, you'll either have to use a nested class or explicitly pass a reference to the outer class to the nested struct.
Dec 04 2017