User Tools

Site Tools


cpp:copy_constructors

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
cpp:copy_constructors [2017/04/20 12:50] – [Ορισμός ενός κατασκευαστή αντιγραφέα] gthanoscpp:copy_constructors [2017/04/20 12:56] – [Δημιουργία κατασκευαστών αντιγραφέων] gthanos
Line 103: Line 103:
 } }
 </code> </code>
-Για τον μεταγλωττιστή όμως οι κώδικες είναι διαφορετικοί. Στην πρώτης περίπτωση καλείται ο κατασκευαστής αντιγραφέας, ενώ στη 2η περίπτωση καλείται ο //default// κατασκευαστής και στη συνέχεια γίνεται ανάθεση των πεδίων του //r1// στα πεδία του //r2//.+Για τον μεταγλωττιστή όμως οι κώδικες είναι διαφορετικοί. Στην πρώτη περίπτωση καλείται ο κατασκευαστής αντιγραφέας (//copy constructor//), ενώ στη 2η περίπτωση καλείται ο //default// κατασκευαστής και στη συνέχεια γίνεται ανάθεση των τιμών των πεδίων του //r1// στα πεδία του //r2//.
 </WRAP> </WRAP>
  
Line 145: Line 145:
     Point *origin;     Point *origin;
   public:   public:
-    Rectangle(int w, int h, Point *p); +    Rectangle(int w, int h, Point p); 
-    Rectangle(int s, Point *p);+    Rectangle(int s, Point p);
     Rectangle();     Rectangle();
 +    ~Rectangle();
     void setWidth(int w);     void setWidth(int w);
     void setHeight(int h);     void setHeight(int h);
     int getWidth();     int getWidth();
     int getHeight();     int getHeight();
 +    void setOrigin(Point *p) { origin = p; }
 +    Point *getOrigin() { return origin; }
 }; };
  
-Rectangle::Rectangle(int w, int h, Point *p) {+Rectangle::Rectangle(int w, int h, Point p) {
   width = w; height = h;   width = w; height = h;
-  origin = p;+  origin = new (nothrow) Point( p.getX(), p.getY() ); 
 +  if(origin == NULL) { 
 +    cerr << "Memory allocation failure!\n"; 
 +    exit(-1); 
 +  }
 } }
  
-Rectangle::Rectangle(int s, Point *p) {+Rectangle::Rectangle(int s, Point p) {
   width = s; height = s;   width = s; height = s;
-  origin = p;+  origin = new (nothrow) Point( p.getX(), p.getY() ); 
 +  if(origin == NULL) { 
 +    cerr << "Memory allocation failure!\n"; 
 +    exit(-1); 
 +  }
 } }
  
Line 172: Line 183:
     exit(-1);     exit(-1);
   }   }
 +}
 +
 +Rectangle::~Rectangle() {
 +  delete origin;
 } }
  
Line 193: Line 208:
  
 int main() { int main() {
-  Point *= new Point(10,5)+  Point p{10,5}
-  Rectangle r1(5,6,p);  +  Rectangle r1{5,6,p};  
   Rectangle r2 = r1;   Rectangle r2 = r1;
      
Line 204: Line 219:
   - Δημιουργεί ένα νέο αντικείμενο τύπου //Point//.   - Δημιουργεί ένα νέο αντικείμενο τύπου //Point//.
   - Αντιγράφει τα περιεχόμενα του παλιού στο νέο.   - Αντιγράφει τα περιεχόμενα του παλιού στο νέο.
 +
 Ο προτεινόμενος κατασκευαστής αντιγραφέας δίνεται παρακάτω: Ο προτεινόμενος κατασκευαστής αντιγραφέας δίνεται παρακάτω:
 <code cpp> <code cpp>
cpp/copy_constructors.txt · Last modified: 2022/05/12 19:41 by gthanos