0
#include<iostream>
using namespace std;

class Something
{
  public:
  int j;
  Something():j(20) {cout<<"Something initialized. j="<<j<<endl;}
};

class Base
{
  private:
    Base(const Base&) {}
  public:
    Base() {}
    virtual Base *clone() { return new Base(*this); }
    virtual void ID() { cout<<"BASE"<<endl; }
};

class Derived : public Base
{
  private:
    int id;
    Something *s;
    Derived(const Derived&) {}
  public:
    Derived():id(10) {cout<<"Called constructor and allocated id"<<endl;s=new Something();}
    ~Derived() {delete s;}
    virtual Base *clone() { return new Derived(*this); }
    virtual void ID() { cout<<"DERIVED id="<<id<<endl; }
    void assignID(int i) {id=i;}
};


int main()
{
        Base* b=new Derived();
        b->ID();
        Base* c=b->clone();
        c->ID();
}//main

On running:

Called constructor and allocated id
Something initialized. j=20
DERIVED id=10
DERIVED id=0

My question is related to this, this and this post.

In the first link, Space_C0wb0y says

"Since the clone-method is a method of the actual class of the object, it can also create a deep-copy. It can access all members of the class it belongs to, so no problems there."

I don't understand how a deep copy can happen. In the program above, not even a shallow copy is happening. I need it to work even if the Base class is an abstract class. How can I do a deep copy here? Help please?

Community
  • 1
  • 1
Nav
  • 19,885
  • 27
  • 92
  • 135
  • Clone() is not something you see (often) in C++. Are you porting a Java application? – Martin York Sep 30 '10 at 14:28
  • No. I'm trying to do a deep copy in C++, and some C++ programmers created their own clone() function as I've shown above. It'll be more clear when you follow the links shown in "My question is related to this, this and this post." (the sentence in quotes is shown in my question above) – Nav Sep 30 '10 at 14:39

1 Answers1

5

Well, your copy constructor does nothing, so your clone method does nothing in the way of copying.

See line Derived(const Derived&) {}

EDIT: if you add code to copy by assignment all members of Derived, it will become a shallow copy. If you also copy (by making a new instance) your instance of Something, it will become a deep copy.

jv42
  • 8,521
  • 5
  • 40
  • 64
  • 1
    +1! The code does not copy anything. Deep copies require that the copying is done manually in the copy constructor. – jwueller Sep 30 '10 at 14:18
  • I see. So the only purpose of having the clone method was to return a pointer of the Base type. A manual copy construction will be required anyway. Sigh! Thanks for the replies :) – Nav Sep 30 '10 at 14:27
  • Small addition: The default copy constructor creates a shallow copy. If you need a shallow copy of something, you do not need to do that manually. – jwueller Sep 30 '10 at 14:29
  • I need a deep copy. That's why I've shown a pointer of the Something class. – Nav Sep 30 '10 at 14:30
  • @Nav: I understood that. I just wanted to point out, that a shallow copy does not require implementing a copy constructor (for the record). – jwueller Sep 30 '10 at 14:38
  • Added these two functions to to the copying: Derived(const Derived& d) {d.setID(id);} and void setID(int i)const {id=i;} but compiler says: test3.cpp: In member function ‘void Derived::setID(int) const’: test3.cpp:34: error: assignment of data-member ‘Derived::id’ in read-only structure. What am I missing? – Nav Sep 30 '10 at 14:50