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:43] gthanos |
cpp:const_member_functions [2021/05/07 07:50] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Const μέθοδοι της κλάσης ====== | ||
| - | |||
| - | Όταν δηλώνεται ένα αντικείμενο ως //const// (όπως παρακάτω), | ||
| - | |||
| - | <code cpp Rectangle.cpp> | ||
| - | #include < | ||
| - | using namespace std; | ||
| - | |||
| - | class Rectangle { | ||
| - | private: | ||
| - | int width, height; | ||
| - | public: | ||
| - | Rectangle(int width, int height); | ||
| - | void setWidth(int width); | ||
| - | void setHeight(int height); | ||
| - | int getWidth(); | ||
| - | int getHeight(); | ||
| - | }; | ||
| - | |||
| - | Rectangle:: | ||
| - | this-> | ||
| - | } | ||
| - | |||
| - | void Rectangle:: | ||
| - | void Rectangle:: | ||
| - | int Rectangle:: | ||
| - | int Rectangle:: | ||
| - | |||
| - | int main() { | ||
| - | const Rectangle rect(10, | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Στον παραπάνω κώδικα επιχειρήστε να διαβάσετε την μεταβλητή //width//, ως εξής: | ||
| - | |||
| - | <code cpp> | ||
| - | int main() { | ||
| - | const Rectangle rect(10,5); | ||
| - | cout << rect.getWidth(); | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Σε αυτή την περίπτωση λαμβάνετε το παρακάτω μήνυμα λάθους: | ||
| - | < | ||
| - | Rectangle5.cpp: | ||
| - | Rectangle5.cpp: | ||
| - | cout << rect.getWidth(); | ||
| - | ^ | ||
| - | </ | ||
| - | |||
| - | Η επεξήγηση του παραπάνω μηνύματος είναι ότι εφόσον το αντικείμενο είναι //const// θα πρέπει και η μέθοδοι που χρησιμοποιούμε για να προσπελάσουμε το αντικείμενο να είναι const, δηλαδή να δηλώνουν ότι δεν μεταβάλλουν το αντικείμενο κατά την εκτέλεση τους. Ο διορθωμένος κώδικας δηλώνει τις συναρτήσεις | ||
| - | |||
| - | <code cpp Rectangle.cpp> | ||
| - | #include < | ||
| - | using namespace std; | ||
| - | |||
| - | class Rectangle { | ||
| - | private: | ||
| - | int width, height; | ||
| - | public: | ||
| - | Rectangle(int width, int height); | ||
| - | void setWidth(int width); | ||
| - | void setHeight(int height); | ||
| - | int getWidth() const; | ||
| - | int getHeight() const; | ||
| - | }; | ||
| - | |||
| - | Rectangle:: | ||
| - | this-> | ||
| - | } | ||
| - | |||
| - | void Rectangle:: | ||
| - | void Rectangle:: | ||
| - | int Rectangle:: | ||
| - | int Rectangle:: | ||
| - | |||
| - | int main() { | ||
| - | const Rectangle rect(10,5); | ||
| - | cout << rect.getWidth(); | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | ===== Υπερφόρτωση const και non-const συναρτήσεων ===== | ||
| - | |||
| - | Στον παραπάνω κώδικα μπορείτε να έχετε δύο εκδόσεις για τις συναρτήσεις getWidth() και getHeight() μία που εφαρμόζεται σε const και μία που εφαρμόζεται σε non-const αντικείμενα ως εξής: | ||
| - | |||
| - | <code cpp Rectangle.cpp> | ||
| - | #include < | ||
| - | using namespace std; | ||
| - | |||
| - | class Rectangle { | ||
| - | private: | ||
| - | int width, height; | ||
| - | public: | ||
| - | Rectangle(int width, int height); | ||
| - | void setWidth(int width); | ||
| - | void setHeight(int height); | ||
| - | int getWidth() const; | ||
| - | int getHeight() const; | ||
| - | int getWidth() ; | ||
| - | int getHeight(); | ||
| - | }; | ||
| - | |||
| - | Rectangle:: | ||
| - | this-> | ||
| - | } | ||
| - | |||
| - | void Rectangle:: | ||
| - | void Rectangle:: | ||
| - | int Rectangle:: | ||
| - | int Rectangle:: | ||
| - | int Rectangle:: | ||
| - | int Rectangle:: | ||
| - | |||
| - | int main() { | ||
| - | const Rectangle rect1(10, | ||
| - | Rectangle rect2(10, | ||
| - | cout << "rect1 width: " << rect1.getWidth() << endl; | ||
| - | cout << "rect2 width: " << rect2.getWidth() << endl; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <WRAP center round tip 80%> | ||
| - | Στο παραπάνω παράδειγμα, | ||
| - | </ | ||
| - | |||
| - | |||
| - | |||