cpp:const_member_functions
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
cpp:const_member_functions [2017/05/15 06:42] – created gthanos | cpp:const_member_functions [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Const μέθοδοι της κλάσης ====== | ====== Const μέθοδοι της κλάσης ====== | ||
- | Όταν δηλώνεται ένα αντικείμενο ως //const// (όπως παρακάτω), | + | Όταν δηλώνεται ένα αντικείμενο ως //const// (όπως παρακάτω), |
<code cpp Rectangle.cpp> | <code cpp Rectangle.cpp> | ||
Line 31: | Line 31: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Στον παραπάνω κώδικα επιχειρήστε να διαβάσετε την μεταβλητή //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%> | ||
+ | Στο παραπάνω παράδειγμα, | ||
+ | </ | ||
cpp/const_member_functions.1494830559.txt.gz · Last modified: 2017/05/15 05:42 (external edit)