jeudi 30 mai 2019

Why i am not able to deep copy a class member

Not able to deep copy one class object to another;

I have a geometry Class object

class Geometry
 {
  public:   
     std::string stdstrType;
     bool bValid;

 public:

  Geometry()
    {
       stdstrType = "NO_TYPE";
       bValid = false;

    }

  Geometry( std::string strType , bool bValue )
  {
    stdstrType = strType;
    bValid = bValue;        
  }

 Geometry(const Geometry &g)
{
    stdstrType = g.stdstrType;
    bValid = g.bValid;
}

 ~Geometry()
 {
     qDebug() << "Geometry  destructor called";
 } 

 virtual void draw();
 bool isValid();
 void setValidState(bool bState);
 virtual void Init();
 std::string GetName(); 

}; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// I have a Container class

 class Container
  {
   private:
    std::string stdstrContainerName;
    std::string stdstrPluginType;
    Geometry* Geom;

  public:
    Container();
    Container(std::string, std::string, Geometry* geometry = nullptr);
    Container(const  Container& obj);
    ~Container();
    std::string GetName();
    std::string GetType();
    void SetName(std::string stdstrName);
    Geometry* GetGeometry();
    void SetGeometry(Geometry* Geom);

  };

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 Container::Container(std::string strName, std::string strType, Geometry* 
 geometry) : Geom(geometry)
{
   stdstrContainerName = strName;
  stdstrPluginType = strType;   
}

 Container::Container(std::string strName, std::string strType, Geometry* 
  geometry) : Geom(geometry)
{
  stdstrContainerName = strName;
  stdstrPluginType = strType;

}


 Container::Container(const Container& obj) {
  stdstrContainerName = obj.stdstrContainerName;
  stdstrPluginType = obj.stdstrPluginType;
  Geom = new Geometry;
  *Geom = *obj.Geom;  // This Line gives error
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

since Container object has a data member with Geometry pointer and the scope of geometry object is shorter than the container object so i want to do a deep copy geometry object inside the Container object.

This is the line in copy constructor which gives me error

*Geom = *obj.Geom; // This Line gives error

This is how i Initilize the Container object

 Geometry* geom = new Geometry;
 Container* cont = new Container("Group", "NONE", geom);

Aucun commentaire:

Enregistrer un commentaire