« VIDEO GAMES - Baldur's Gate: Dark Alliance II | Main | DIGITAL PHOTOGRAPHY - some PR Xmas '03 photos »

February 05, 2004

Comments

Grafik

Assuming that calling memset is any faster than just setting the individual fields to zero (not likely under most circumstances)...

The better way to use it is to only set the real fields of the object not the complete object. For example:

struct Blah : public SuperBlah {
int a;
int b;
...
}

void Blah::reset()
{
SuperBlah::reset();
memset(((char*)this)+
offsetof(Blah::a),0,
offsetof(Blah::b)+sizeof(Blah::b)-
offsetof(Blah::a)
);
}

That way you just don't mess with the internal data of the instance, the object is still useable after it's reset, no danger of double deletes, the object is useable in heaps/arrays/stack, and it lets you safely subclass.

crowdpleazr

I generally write reset functions for my classes that are also used in constructors as class memeber initialization routines. If I were to just run into this class, and inherit from it, I could end up in a recursive loop of construct->reset->construct->reset.

This code is so fucking ugly, and anyone that uses it should be shot.

This code also breaks some cardinal rules I have for data structures. Anything that is pure data should be a struct, and anything that has member functions should be a class. I've never been in a situation where I really wanted to inherit from a struct, using these rules.

Not that I'd ever want to do this, but if your friend is set in doing it this way, the safest solution is to create the class he needs with member functions to operate on the data, then create the data as a seperate struct, encapsulated within the class. That way he can call a memset on just the struct without destroying his vtable. MUCH safer.

Maybe you should get this guy a PWG shirt "Your code sucks"

The comments to this entry are closed.