Objekt kopieren



  • wie kann ich eine objekt mit inhalt kopieren?
    der strukt sieht folgendermaßen aus:

    typedef struct  {
    	     uint Width;
    	     uint Height;
    	     uint Depth;
    	     QGLColormap *Colormap;
    	     int VoxelWidth;
    	     int VoxelHeight;
    	     int VoxelDepth;
    	     uint Value;
    	  } Volume;
    

    also z.B. ich hab das object Volumen1 und möchte es in Volumen 2 kopieren
    Danke!

    Lg Devil's Daughter



  • Hallo,

    erstmal programmierst du ab jetzt C++ 😉 Dann überlädst du den Zuweisungs-operator:

    struct Volume  {
      uint Width;
      uint Height;
      uint Depth;
      QGLColormap *Colormap;
      int VoxelWidth;
      int VoxelHeight;
      int VoxelDepth;
      uint Value;
    
      Volume& operator=(const Volume &rhs) {
        this->Width = rhs.Width;
        //usw.
    
        return *this;
      }
    }; 
    
    //Jetzt kannst schreiben:
    Volume v1, v2;
    
    v1 = v2; //kopiere v2 in v1
    

    Siehe auch: http://tutorial.schornboeck.net/operatoren_ueberladung.htm

    MfG

    GPC



  • Vielen Dank!

    🙂



  • Und solange du keine tiefe Kopie für den Zeiger "Colormap" verwenden willst, reicht auch der vom Compiler erzeugte Zuweisungsoperator aus.


Log in to reply