cpp:copy_constructors
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cpp:copy_constructors [2017/04/20 11:44] – gthanos | cpp:copy_constructors [2017/04/20 12:20] – [Ορισμός ενός κατασκευαστή αντιγραφέα] gthanos | ||
---|---|---|---|
Line 76: | Line 76: | ||
int Rectangle:: | int Rectangle:: | ||
</ | </ | ||
+ | |||
+ | <WRAP center round tip 60%> | ||
+ | Εάν δεν ορίσετε ένα δικό σας κατασκευαστή αντιγραφέα ο // | ||
+ | </ | ||
+ | |||
+ | Μία άλλη περίπτωση κατά την οποία θα κληθεί o κατασκευαστής αντιγραφέας είναι η παρακάτω. Εδώ μαζί με την δήλωση της μεταβλητής γίνεται ανάθεση των πεδίων του αντικειμένου //r1// στο // | ||
+ | |||
+ | <code cpp CopyRectangle.cpp> | ||
+ | #include " | ||
+ | |||
+ | int main() { | ||
+ | Rectangle r1(5,6); | ||
+ | Rectangle r2 = r1; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Δημιουργία κατασκευαστών αντιγραφέων ==== | ||
+ | |||
+ | Στις περιπτώσεις που υπάρχουν πεδία δείκτες που δείχνουν σε άλλα αντικείμενα (στατικά ή δυναμικά δεσμευμένα) αντιγράφονται οι διευθύνσεις αυτές, όπως θα αντιγράφονταν οποιοδήποτε άλλο πεδίο. Αυτό πρακτικά σημαίνει ότι δύο ή περισσότερα αντικείμενα δείχνουν σε μία κοινή περιοχή μνήμης. Το παραπάνω μπορεί να προκαλέσει δυσλειτουργίες καθώς η μεταβολή του κοινού αντικειμένου επηρεάζει το σύνολο των αντικειμένων που το μοιράζονται. | ||
+ | |||
+ | Στο παρακάτω παράδειγμα ορίζουμε την κλάση //Point// η οποία αντιπροσωπεύει ένα σημείο στο διδιάστατο χώρο. | ||
+ | |||
+ | <code cpp Point.cpp> | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | class Point { | ||
+ | int x, y; | ||
+ | public: | ||
+ | Point(int vx,int vy) { x = vx; y = vy; cout << "Point regular constructor!\n"; | ||
+ | Point(const Point &p) { x = p.x; y = p.y; cout << "Point copy constructor!\n"; | ||
+ | Point() { cout << "Point default constructor!\n"; | ||
+ | ~Point() { cout << "xP Point destructor!\n"; | ||
+ | void setX(int vx) { x = vx; } | ||
+ | void setY(int vy) { y = vy; } | ||
+ | int getX() { return x; } | ||
+ | int getY() { return y; } | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | Η κλάση // | ||
+ | |||
+ | <code cpp Rectangle.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | #include " | ||
+ | |||
+ | class Rectangle { | ||
+ | private: | ||
+ | int width, height; | ||
+ | Point *origin; | ||
+ | public: | ||
+ | Rectangle(int w, int h, Point *p); | ||
+ | Rectangle(int s, Point *p); | ||
+ | Rectangle(); | ||
+ | void setWidth(int w); | ||
+ | void setHeight(int h); | ||
+ | int getWidth(); | ||
+ | int getHeight(); | ||
+ | }; | ||
+ | |||
+ | Rectangle:: | ||
+ | width = w; height = h; | ||
+ | origin = p; | ||
+ | } | ||
+ | |||
+ | Rectangle:: | ||
+ | width = s; height = s; | ||
+ | origin = p; | ||
+ | } | ||
+ | |||
+ | Rectangle:: | ||
+ | srand(time(NULL)); | ||
+ | width = rand() % 10 + 1; height = rand() % 10 + 1; | ||
+ | origin = new (nothrow) Point{}; | ||
+ | if(origin == NULL) { | ||
+ | cerr << " | ||
+ | exit(-1); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void Rectangle:: | ||
+ | void Rectangle:: | ||
+ | int Rectangle:: | ||
+ | int Rectangle:: | ||
+ | </ | ||
+ | |||
+ | <code cpp MoveOrigin.cpp> | ||
+ | |||
+ | #include " | ||
+ | |||
+ | |||
+ | |||
+ | int main() { | ||
+ | Point *p = new Point(10, | ||
+ | Rectangle r1(5,6,p); | ||
+ | | ||
+ | Rectangle rect(5, | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ |
cpp/copy_constructors.txt · Last modified: 2022/05/12 19:41 by gthanos