S
Stephan Br?nnimann
Dear all
thanks to Scott Meyers (ME C++, item 33) we know assignment operators
should be protected in the base class.
Is there a common pattern (similar to virtual construction via `clone()')
that allows me to have the following:
(std::auto_ptr and covariant returns omitted for simplicity.)
class Base {
protected:
Base(const Base& rhs);
Base& operator=(const Base& rhs);
public:
...
Base* clone() const = 0;
};
class Leaf : public Base {
public:
...
Base* clone() const;
};
void save(Base& base)
{
Base* backup = base->clone();
try {
write(base); // may modify `base', throw in case of an error
}
catch (...) {
// restore original status of `base'
base = *backup; // <-- does (intentionally) not work
}
}
Of course the following
class Base {
public:
void backup() = 0;
void restore() = 0;
};
solves the problem, but is there a common way for such assignments?
Stephan Brönnimann
(e-mail address removed)
Open source rating and billing engine for communication networks.
thanks to Scott Meyers (ME C++, item 33) we know assignment operators
should be protected in the base class.
Is there a common pattern (similar to virtual construction via `clone()')
that allows me to have the following:
(std::auto_ptr and covariant returns omitted for simplicity.)
class Base {
protected:
Base(const Base& rhs);
Base& operator=(const Base& rhs);
public:
...
Base* clone() const = 0;
};
class Leaf : public Base {
public:
...
Base* clone() const;
};
void save(Base& base)
{
Base* backup = base->clone();
try {
write(base); // may modify `base', throw in case of an error
}
catch (...) {
// restore original status of `base'
base = *backup; // <-- does (intentionally) not work
}
}
Of course the following
class Base {
public:
void backup() = 0;
void restore() = 0;
};
solves the problem, but is there a common way for such assignments?
Stephan Brönnimann
(e-mail address removed)
Open source rating and billing engine for communication networks.