By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

"Assignment" through base class

P: n/a
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
br****@osb-systems.com
Open source rating and billing engine for communication networks.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 8 Aug 2004 23:14:27 -0700, Stephan Br?nnimann <br****@hotmail.com>
wrote:
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
Jul 22 '05 #2

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<opscgcpflk212331@andronicus>...
On 8 Aug 2004 23:14:27 -0700, Stephan Br?nnimann <br****@hotmail.com>
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
br****@osb-systems.com
Open source rating and billing engine for communication networks.

BTW: thanks for the corrections.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.