www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct inside a class: How to get outer?

reply "Nick Sabalausky (Abscissa)" <SeeWebsiteToContactMe semitwist.com> writes:
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
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
next sibling parent reply bauss <jj_1337 live.dk> writes:
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:
 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
It wouldn't make much sense either, if a struct was able to do it.
Dec 03
parent reply "Nick Sabalausky (Abscissa)" <SeeWebsiteToContactMe semitwist.com> writes:
On 12/03/2017 03:46 AM, bauss wrote:
 On Sunday, 3 December 2017 at 07:38:47 UTC, Jonathan M Davis wrote:
 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.
Why is that?
Dec 03
parent Jacob Carlborg <doob me.com> writes:
On 2017-12-03 10:57, Nick Sabalausky (Abscissa) wrote:
 On 12/03/2017 03:46 AM, bauss wrote:
 It wouldn't make much sense either, if a struct  was able to do it.
Why is that?
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 Carlborg
Dec 03
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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:
 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.
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. -Steve
Dec 04
parent A Guy With a Question <aguywithanquestion gmail.com> writes:
On Monday, 4 December 2017 at 14:01:08 UTC, Steven Schveighoffer 
wrote:
 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:
 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.
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. -Steve
Yes! Don't forget the 'static' otherwise you'll get some odd errors.
Dec 04