Copying data allocated by new

I have an abstract class Base, with derived classes Derived1, Derived2, etc. I don't know how many there are. So, I have declared an object of Derived like so:
Base* der1 = new Derived1(/* constructor details */);

That gets passed to a function, which modified the data contained by this pointer. However, I need to keep the data from the object, which means that I need to copy the data somehow. The problem is, this copying needs to be done within the function, due to the requirements of the program. I do not know what type the object is, This function will need to reset this data potentially hundreds of times, so I can't just provide lots of objects, as either the function will run out of objects to call or I will run out of space in memory.

How would I create a copy of this, so that I would be modifying a temporary object that could be deleted and I would keep the data that I started with?
I think you have a big problem in how the program is designed.

My suggestion will probably turn out to be stupid, so you should wait for replies from more experienced members.

I think you should use typeid and then copy, after downcasting?
http://www.cplusplus.com/reference/typeinfo/type_info/operator==/

1
2
3
4
5
6
7
8
9
void func(Base *b)
{
    if (typeid(*b) == typeid(Derived))
    {
        Derived d = *dynamic_cast<Derived *> (b);

        // ... work with d ...
    }
}

I had already considered this, but because of the number of possible derived classes I had rejected this idea, and the fact that (from what I know) casting in that way can be too slow (and this function is already a bottleneck for program execution). However, as I do more research, I think that this will be probably be the way I have to go.
To polymorphically make a copy of an object, use a "virtual copy constructor".
http://www.parashift.com/c++-faq/virtual-ctors.html

To externalise the the state of an object without violating its encapsulation, and later restore the object to a previous state, use the memento pattern.
http://sourcemaking.com/design_patterns/memento

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <memory>
#include <string>
#include <iostream>

struct base
{
    struct state { virtual ~state() {} } ;
    using memento = std::shared_ptr<state> ;

    virtual ~base() {}
    virtual memento current_state() const = 0 ;
    virtual void restore_state( const memento& m ) = 0 ;

    virtual std::unique_ptr<base> clone() const = 0 ;

    virtual std::ostream& print( std::ostream& ) const = 0 ;
    // ...

    friend inline std::ostream& operator<< ( std::ostream& stm, const base& b )
    { return b.print(stm) ; }

    protected:
        base() = default ;
        base( const base& ) = default ;
        base( base&& ) = default ;
        base& operator= ( const base& ) = delete ;
};

struct derived : base
{
    derived( const std::string& name, int value ) : name_(name), value_(value) {}

    std::string name() const { return name_ ; }
    void name( const std::string& name ) { name_ = name ; }

    int value() const { return value_ ; }
    void value( int value ) { value_ = value ; }

    virtual memento current_state() const override
    { return memento{ new state( name(), value() ) }; }

    virtual void restore_state( const memento& m ) override
    {
        auto s = dynamic_cast< const state* >( m.get() ) ;
        if(!s) throw std::domain_error( "invalid state " ) ;
        name_ = s->name ;
        value_ = s->value ;
    }

    virtual std::unique_ptr<base> clone() const override
    { return std::unique_ptr<base>{ new derived(*this) } ; }

    virtual std::ostream& print( std::ostream& stm ) const override
    { return stm << "derived{ '" << name() << "', " << value() << " }" ; }

    // ...

    private:
        class state : public base::state
        {
            state( const std::string& name, int value ) : name(name), value(value) {}
            std::string name ;
            int value ;
            friend class derived ;
        };

        std::string name_ ;
        int value_ ;

        derived( const derived& ) = default ;
        derived( derived&& ) = default ;
};

void foo( const base* b ) 
{
    std::cout << "original: " << *b << '\n' ;
    auto clone = b->clone() ;
    std::cout << "clone: " << *clone << '\n' ;
}

int main()
{
    derived d { "test", 1234 } ;

    base* b = &d ;
    foo(b) ;

    auto m = b->current_state() ;

    d.name( "*** modified *** " ) ;
    d.value( -9999 ) ;

    std::cout << *b << '\n' ;

    b->restore_state(m) ;
    std::cout << *b << '\n' ;
}

http://coliru.stacked-crooked.com/a/f9f13ef470fedbbf
That is exactly what I was looking for! Thankyou!
Topic archived. No new replies allowed.