Topic: Cloning object of derived class from base pointer?


Author: postmast.root.admi.gov@iname.com (blargg)
Date: 1999/09/16
Raw View
[I added comp.std.c++, as this relates to the standard]

In article <AOu1vkAhQt33EwNf@robinton.demon.co.uk>, Francis Glassborow
<francisG@robinton.demon.co.uk> wrote:

> In article <user-1309991527220001@aus-as3-071.io.com>, blargg <postmast.
> root.admi.gov@iname.com> writes
> >
> >OK so far, but...
> >
> >    struct D2 : D {
> >        // oops, forgot clone()!
> >    };
> >
> >No error. Solutions to this are wordy at best.
>
> Which is why I would like to see implementors experiment with a language
> extension that provides a method that requires redefinition of a
> (virtual) function in _every_ derived class that is not an ABC.  With
> sufficient experience we could consider its inclusion in the next
> version of C++

Yes.

This would also be useful for persistence:

    class Base {
    protected:
        virtual void read( istream& ) = 0;
        virtual void write( ostream& ) const = 0;
    public:
        friend ostream& operator << ( ostream& str, Base const& obj ) {
            obj.write( str );
            return str;
        }
        friend istream& operator >> (istream& str, Base& obj ) {
            read( str );
            return str;
        }
    };

    class Derived : public Base {
    protected:
        virtual void read( istream& );
        virtual void write( ostream& ) const;
    public:
        // ...
    };

    class Derived2 : public Derived {
    protected:
        virtual void read( istream& );
        // oops, forgot write()!
    public:
        // ...
    };

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]