Reputation: 1736
For a class with subclasses or sctructures inside, what's the most ellegant way of reseting it?
class attributes {
public:
std::string address;
short port;
std::vector< std::string > data;
struct Foo foo;
};
what's is the most elegant in a loop, default operator = ou create an Reset method memoring 0 the structure memset(...)
?
attributes obj, originalStateToResetObj;
for(;;)
//do stuff with obj
obj.address = "172.0.0.1"
//etc
//reseting obj using operator=
obj = originalStateToResetObj;
// OR using Reset?
obj.Reset();
}
Other elegant suggestion?
Upvotes: 0
Views: 1103
Reputation: 60748
This completely depends on the class at hand. Generally the right thing to do would be create a new one from scratch or use the class's copy constructor, but if it's using expensive resources, e.g. opening an IMAP connection (this example comes to mind since IMAP servers often limit the number of connections allowed per user quite strictly), then this can have very unwanted side-effects.
If a class has no zero-argument constructor then it makes little sense to "reset" it to some default state, so you will need to consider that as well.
Consider Memento design pattern if there is meaningful intermediate state you do need to retain.
Upvotes: 2