Reset class member variables

mandel mandel at foobar.com
Fri Sep 7 11:57:08 PDT 2007


Yes ,that works (and looks horrific, too). :P

Sean Kelly Wrote:

> mandel wrote:
> > Hi,
[..]
> > class Foo
> > {
> > 	uint x;
> > 	char[] name = "world";
> > //problematic:
> > 	const uint y;
> > 	char[1024] buffer;
> > 	
> > 	void reset()
> > 	{
> > 		scope tmp = new typeof(this);
> > 		foreach(i, x;  tmp.tupleof)
> > 		{
> > 			this.tupleof[i] = x;
> > 		}
> > 	}
> > }
> > 
> > The problem is that I have to avoid
> > to try to set const values and static arrays.
> > 
> > How can this be done?
> 
> It's a bit horrifying, but this should work:
> 
> class Foo
> {
>      uint x;
>      char[] name = "world";
> //problematic:
>      const uint y;
>      char[1024] buffer;
> 
>      void reset()
>      {
>          (cast(byte*) this)[0 .. this.classinfo.init.length] =
>              this.classinfo.init[0 .. $];
>      }
> }
> 
> 
> Sean




More information about the Digitalmars-d mailing list