"Assignment" through base class

Discussion in 'C++' started by Stephan Br?nnimann, Aug 9, 2004.

  1. 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

    Open source rating and billing engine for communication networks.
    Stephan Br?nnimann, Aug 9, 2004
    #1
    1. Advertising

  2. On 8 Aug 2004 23:14:27 -0700, Stephan Br?nnimann <>
    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
    John Harrison, Aug 9, 2004
    #2
    1. Advertising

  3. "John Harrison" <> wrote in message news:<opscgcpflk212331@andronicus>...
    > On 8 Aug 2004 23:14:27 -0700, Stephan Br?nnimann <>
    > 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

    Open source rating and billing engine for communication networks.

    BTW: thanks for the corrections.
    Stephan Br?nnimann, Aug 9, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Heinz Buchegger
    Replies:
    3
    Views:
    522
    Karl Heinz Buchegger
    Aug 6, 2003
  2. John Harrison
    Replies:
    0
    Views:
    482
    John Harrison
    Aug 6, 2003
  3. user
    Replies:
    1
    Views:
    392
    Jack Klein
    Jul 11, 2005
  4. Replies:
    11
    Views:
    573
  5. siddhu
    Replies:
    12
    Views:
    621
    Gianni Mariani
    Jun 6, 2007
Loading...

Share This Page