www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D2 Pointer confusion

reply A Bothe <info alexanderbothe.com> writes:
Hey guys,
I got a problem with the following code:

void main()
{
	struct SomeStruct
	{
		int aa;
	}
	
	class SomeClass
	{
		int a;
		
		SomeStruct foo()
		{
			SomeStruct m;
			m.aa=a;
			return m;
		}
	}
	
	SomeClass inst=new SomeClass();
	inst.foo.aa=20;
	
	assert(inst.a==20);
}


How can I make D set the member variable 'a' of SomeClass via this construction
to another value?
And what's with using "ref SomeStruct foo() {}"?

Thanks in advance!

------------------------------------------------------------------------------
PS: Check out my D-IDE at http://www.alexanderbothe.com/?id=27
Aug 30 2009
parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Umm.. A shot in the dark?

void main(){
   struct SomeStruct{
      int* p;
      void aa(int a){
        *p = a;
      }
   }
   class SomeClass{
      int a;
      SomeStruct foo(){
         SomeStruct m;
         m.g = &a;
         return m;
      }
   }
   SomeClass inst = new SomeClass;
   inst.foo.aa = 20;
   assert(inst.a == 20);
}

A Bothe wrote:
 Hey guys,
 I got a problem with the following code:
 
 void main()
 {
 	struct SomeStruct
 	{
 		int aa;
 	}
 	
 	class SomeClass
 	{
 		int a;
 		
 		SomeStruct foo()
 		{
 			SomeStruct m;
 			m.aa=a;
 			return m;
 		}
 	}
 	
 	SomeClass inst=new SomeClass();
 	inst.foo.aa=20;
 	
 	assert(inst.a==20);
 }
 
 
 How can I make D set the member variable 'a' of SomeClass via this
construction to another value?
 And what's with using "ref SomeStruct foo() {}"?
 
 Thanks in advance!
 
 ------------------------------------------------------------------------------
 PS: Check out my D-IDE at http://www.alexanderbothe.com/?id=27
Sep 01 2009