Re: Shallow\Deep copy
ManicQin wrote:
Hi, I'll do my best to explain my problem...
I have an data type that is wrapping a vector of pointers, currently I
have no control of the type that the vector holds and I need to be as
generic as I can (it's a part of a frame work).
naturally my vector is templated by the client.
I tried to think of a way to deep copy the vector into another -
without<- adding Clone function and such to the objects and I couldnt
think of one, I would be happy if anyone knows of a way...
Presumably your class looks like this:
template <typename T>
class C {
public:
// stuff
private:
std::vector <T*> v;
};
in which case why not add a copy constructor that "deep copies" by using
the copy constructor of the things pointed to by the vector:
C( const C & c ) {
for( int i = 0; i < c.v.size(); i++ ) {
v.push_back( new T( *c.v.at(i) ));
}
}
This of course does not give you polymorphic copying - if you want that,
I think forcing your clients to supply a virtual Clone() function is the
most sensible solution.
Neil Butterworth
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]