"Assignment" through base class

  • Thread starter Stephan Br?nnimann
  • Start date
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.
 
J

John Harrison

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();

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
}

delete backup;

Wouldn't it be easier to call write on the copy if you are worried about
modifying the original?

void save(Base& base)
{
Base* copy = base.clone();
try {
write(copy);
}
catch (...) {
}
delete copy;
}

Maybe I'm missing the point.

john
 
S

Stephan Br?nnimann

John Harrison said:
On 8 Aug 2004 23:14:27 -0700, Stephan Br?nnimann <[email protected]>
wrote:
[snip]

Wouldn't it be easier to call write on the copy if you are worried about
modifying the original?

void save(Base& base)
{
Base* copy = base.clone();
try {
write(copy);
}
catch (...) {
}
// success: write back to original
base = *copy; // won't work
delete copy;
}

Maybe I'm missing the point.

john

In the simple situation that I have described: yes that's the solution.

Things however are more complicated: if the write operation succeeds
the object that was stored must survive. This requires the assignment
of the copy to the original ...

Stephan Brönnimann
(e-mail address removed)
Open source rating and billing engine for communication networks.

BTW: thanks for the corrections.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top