Question: Consider the following code in C++: Here *Lp will be a deep copy of L, containing a copy of each foo object. Try to write

Consider the following code in C++:

template  class cloneable_list : public list { public: cloneable_list* clone () { auto rtn = new cloneable_list(); for (auto e : *this) { rtn->push_back (e); return rtn; }; cloneable_list L; cloneable_list* Lp = L.clone (); %3D

Here *Lp will be a “deep copy” of L, containing a copy of each foo object. Try to write equivalent code in Java. What goes wrong? How might you get around the problem?

template class cloneable_list : public list { public: cloneable_list * clone ()

template class cloneable_list : public list { public: cloneable_list * clone () { auto rtn = new cloneable_list (); for (auto e : *this) { rtn->push_back (e); return rtn; }; cloneable_list L; cloneable_list * Lp = L.clone (); %3D

Step by Step Solution

3.40 Rating (159 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

In Java the equivalent code would be class CloneableList extends ArrayList public CloneableList clon... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!