0

I was checking the factory design pattern in c++. I understood why we use the cloning method:

Although genetic factories producing clones of the universal soldier are quite a scary prospect, cloning C objects is a harmless and useful activity most of the time. Here the goal is slightly different from what we dealt with so far: We don’t have to create objects from scratch anymore. We have a pointer to a polymorphic object, and we’d like to create an exact copy of it. Because we don’t exactly know the type of the polymorphic object, we don’t exactly know what new object to create, and this is the actual issue. Quote

The part that I didn't understand is return new Line(*this); in function virtual Line* Clone() const. Can you please tell me If my interpretation is correct.

  • In Shape* Create(const std::string& key) const we call tmp=((*it).second)->Clone();
  • Since we want an exact copy, what we did in here is call the copy constructor Line(const Line &) and pass this as argument. But since this a pointer we have to dereference it because we are passing it by reference to the copy constructor.
  • Suppose we use return new Line() instead of return new Line(*this) what will happen? we wont be returning a copy of the object but a new object? which is kind of stupid and wrong. Why create a new object since it already exists

public:
virtual Shape* Clone() const = 0;
...
};
class Line : public Shape
{
public:
virtual Line* Clone() const
{
return new Line(*this);
}
...
};

//The create function is from the Factory
    Shape* Create(const std::string& key) const
    {
         Figure* tmp=0;
         std::map<string, Figure*>::const_iterator it=m_map.find(key);

          if(it!=m_map.end())
          {
               tmp=((*it).second)->Clone();
          }
       return tmp;
    }

gringo
  • 373
  • 2
  • 4
  • 15
  • Do you understand the difference between deep and shallow copies? (Where's the quote from BTW?) – doctorlove Aug 31 '18 at 11:47
  • @doctorlove yes Suppose we have class `A` with a member which is a pointer and we it to create a dynamic array. Now suppose we instantiate an object of class `A`. `A o1` and fill the array. If we want to assign `o1` to another object `o2`. `A o2 = o1` and we don't override the copy constructor then the **addresses** will be copied instead of the **VALUES** inside the array. This is a problem because if we change the value of `o1` then the **values** in `o2` will be changed we don't want that. This is why we need to override the copy constructor for not allow a shallow copy – gringo Aug 31 '18 at 11:56
  • https://stackoverflow.com/questions/39317227/c-copy-constructor-using-pointers this link is cool too – gringo Aug 31 '18 at 14:52

2 Answers2

1

The expression new Line(*this) creates a new Line object and construct the new object using the Line copy-constructor.

That should make an exact copy of *this, i.e. you clone *this.

Of course, for it all to work consider the rules of three, five and zero.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Suppose we use return new Line() instead of return new Line(*this) what will happen?

Ans : It will create new object of Line created using default constructor which may not be same as the object using which you are calling the clone method. So, if you want clone object to the calling object you need to create object using copy constructor i.e using code new Line(*this)

dheeraj Vadlani
  • 377
  • 2
  • 13