This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
cpp:const_member_functions [2017/05/15 07:18] gthanos |
cpp:const_member_functions [2021/05/07 07:50] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Const μέθοδοι της κλάσης ====== | ||
| - | |||
| - | Όταν δηλώνεται ένα αντικείμενο ως //const// (όπως παρακάτω), | ||
| - | |||
| - | <code cpp Point.cpp> | ||
| - | #include < | ||
| - | using namespace std; | ||
| - | |||
| - | class Point { | ||
| - | int x, y; | ||
| - | public: | ||
| - | Point(int vx,int vy) { x = vx; y = vy; } | ||
| - | 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(); | ||
| - | ~Rectangle(); | ||
| - | void setOrigin(Point &p); | ||
| - | Point & | ||
| - | }; | ||
| - | |||
| - | Rectangle:: | ||
| - | width = w; height = h; | ||
| - | origin = new (nothrow) Point( p.getX(), p.getY() ); | ||
| - | if(origin == NULL) { | ||
| - | cerr << " | ||
| - | exit(-1); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | Rectangle:: | ||
| - | delete origin; | ||
| - | } | ||
| - | |||
| - | void Rectangle:: | ||
| - | if(origin!=NULL) | ||
| - | delete origin; | ||
| - | | ||
| - | origin = new (nothrow) Point( p.getX(), p.getY() ); | ||
| - | if(origin == NULL) { | ||
| - | cerr << " | ||
| - | exit(-1); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | Point & | ||
| - | |||
| - | int main() { | ||
| - | const Point p(5,6); | ||
| - | const Rectangle rect(1, | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Στον παραπάνω κώδικα επιχειρήστε να διαβάσετε το πεδίο //origin//, ως εξής: | ||
| - | |||
| - | <code cpp> | ||
| - | int main() { | ||
| - | const Point p(5,6); | ||
| - | const Rectangle rect(1, | ||
| - | Point p1 = rect.getOrigin(); | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Σε αυτή την περίπτωση λαμβάνετε το παρακάτω μήνυμα λάθους: | ||
| - | < | ||
| - | Rectangle.cpp: | ||
| - | Rectangle.cpp: | ||
| - | const Point p1 = rect.getOrigin(); | ||
| - | ^ | ||
| - | </ | ||
| - | |||
| - | Η επεξήγηση του παραπάνω μηνύματος είναι ότι εφόσον το αντικείμενο είναι //const// θα πρέπει και οι μέθοδοι που χρησιμοποιούμε για να προσπελάσουμε το αντικείμενο να είναι δηλωμένες ως //const//, δηλαδή να δηλώνουν ότι δεν μεταβάλλουν το αντικείμενο κατά την εκτέλεση τους. Ο διορθωμένος κώδικας δηλώνει ως //const// τη συνάρτηση // | ||
| - | |||
| - | <code cpp> | ||
| - | Point & | ||
| - | </ | ||
| - | |||
| - | ===== Επιστρεφόμενη τιμή const συναρτήσεων ===== | ||
| - | |||
| - | Στον παραπάνω κώδικα αν και η συνάρτηση // | ||
| - | |||
| - | <code cpp> | ||
| - | int main() { | ||
| - | const Point p(5,6); | ||
| - | const Rectangle rect(1, | ||
| - | Point p1 = rect.getOrigin(); | ||
| - | p1.setX(p1.getX()+1); | ||
| - | } | ||
| - | <code cpp> | ||
| - | |||
| - | Για να αποφύγουμε τα παραπάνω πρόβληματα είναι καλό να δηλώσουμε την επιστρεφόμενη τιμή της getOrigin ως //const//. Σε αυτή την περίπτωση η επιστρεφόμενη τιμή θα πρέπει με τη σειρά της να ανατεθεί σε ένα αντικείμενο τύπου const. Ο συνολικός κώδικας έχει ως εξής: | ||
| - | |||
| - | <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(); | ||
| - | ~Rectangle(); | ||
| - | void setOrigin(Point &p); | ||
| - | const Point & | ||
| - | }; | ||
| - | |||
| - | Rectangle:: | ||
| - | width = w; height = h; | ||
| - | origin = new (nothrow) Point( p.getX(), p.getY() ); | ||
| - | if(origin == NULL) { | ||
| - | cerr << " | ||
| - | exit(-1); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | Rectangle:: | ||
| - | delete origin; | ||
| - | } | ||
| - | |||
| - | void Rectangle:: | ||
| - | if(origin!=NULL) | ||
| - | delete origin; | ||
| - | | ||
| - | origin = new (nothrow) Point( p.getX(), p.getY() ); | ||
| - | if(origin == NULL) { | ||
| - | cerr << " | ||
| - | exit(-1); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | const Point & | ||
| - | |||
| - | int main() { | ||
| - | const Point p(5,6); | ||
| - | const Rectangle rect(1, | ||
| - | const Point p1 = rect.getOrigin(); | ||
| - | } | ||
| - | </ | ||
| - | int main() { | ||
| - | const Point p(5,6); | ||
| - | const Rectangle rect(1, | ||
| - | const Point p1 = rect.getOrigin(); | ||
| - | } | ||
| - | </ | ||
| - | < | ||