Reset class member variables

Sean Kelly sean at f4.ca
Sat Sep 8 07:38:22 PDT 2007


Nathan Reed wrote:
> mandel wrote:
>> It's nice not to have to sacrifice speed for reliability.
> 
> In a garbage collected language like D, allocations are extremely fast, 
> potentially several times faster than heap based languages like C/C++.
> 
> Using a new instance of the object for each iteration of the loop is 
> really what you're doing /conceptually/, anyway...so, I wouldn't worry 
> about the allocation performance too much unless you've profiled the app 
> and established that it is a bottleneck.

Or if there's some reason that the object must be re-initialized in 
place--say, to preserve its address.  One could argue that this 
necessity indicates a poor program design, but I imagine there are cases 
where it may apply.

For what it's worth, this would be quite easy to accomplish if D 
supported placement new as a default construction method (rather than 
requiring the user to override operator new to do so).  Then, 
re-initializing an object would be as simple as:

this = new(this) typeof(this);

(assuming the runtime doesn't do anything too weird in _d_new)

> And if that's the case you might want to think about making the object a 
> struct (so a value-type, allocated on the stack) anyway.

Good point.


Sean



More information about the Digitalmars-d mailing list