Suppose I have an object with a member that is expensive to construct, and the need for a reset()
function that resets it to its initial state:
struct Example
{
// option 1: efficient, but need to duplicate initialization logic of reset()
Example(int param) : expensive_member_(param), param_(param)
{
}
/* option 2: less code, but initializes expensive_member_ twice
Example(int param) : param_(param)
{
reset();
}*/
void reset()
{
expensive_member_ = ClassWithExpensiveConstructor(param_);
}
ClassWithExpensiveConstructor expensive_member_;
int param_;
}
Is there a better way of/pattern for efficiently resetting the object to its initial state without duplicating the initialization logic in the initializer list and the reset
function?
Aucun commentaire:
Enregistrer un commentaire