User Tools

Site Tools


cpp:const_member_functions

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cpp:const_member_functions [2017/05/15 07:41]
gthanos
cpp:const_member_functions [2021/05/07 07:50]
Line 1: Line 1:
-====== Const μέθοδοι της κλάσης ====== 
- 
-Όταν δηλώνεται ένα αντικείμενο ως //const// (όπως παρακάτω), τότε οι μεταβλητές του αντικειμένου μπορούν μόνο να διαβαστούν αλλά όχι να μεταβληθούν, πρόκειται δηλαδή για ένα αμετάβλητο αντικείμενο. Παρατηρήστε βέβαια, ότι ο κατασκευαστής του αντικειμένου αρχικοποιεί κανονικά το αντικείμενο και μεταβάλλει τις μεταβλητές του. 
- 
-<code cpp Rectangle.cpp> 
-#include <iostream> 
-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::Rectangle(int width, int height) { 
-  this->width = width; this->height = height; 
-} 
- 
-void Rectangle::setWidth(int width) { this->width = width; } 
-void Rectangle::setHeight(int height) { this->height = height; } 
-int Rectangle::getWidth() { return width; } 
-int Rectangle::getHeight() { return height; } 
- 
-int main() { 
-  const Rectangle rect(10,5);   
-} 
-</code> 
- 
-Στον παραπάνω κώδικα επιχειρήστε να διαβάσετε την μεταβλητή //width//, ως εξής: 
- 
-<code cpp> 
-int main() { 
-  const Rectangle rect(10,5); 
-  cout << rect.getWidth(); 
-} 
-</code> 
- 
-Σε αυτή την περίπτωση λαμβάνετε το παρακάτω μήνυμα λάθους: 
-<code> 
-Rectangle5.cpp: In function ‘int main()’:                                                                                                                                                                           
-Rectangle5.cpp:26:25: error: passing ‘const Rectangle’ as ‘this’ argument of ‘int Rectangle::getWidth()’ discards qualifiers [-fpermissive]                                                                         
-   cout << rect.getWidth();                                                                                                                                                                                         
-                         ^ 
-</code> 
- 
-Η επεξήγηση του παραπάνω μηνύματος είναι ότι εφόσον το αντικείμενο είναι //const// θα πρέπει και η μέθοδοι που χρησιμοποιούμε για να προσπελάσουμε το αντικείμενο να είναι const, δηλαδή να δηλώνουν ότι δεν μεταβάλλουν το αντικείμενο κατά την εκτέλεση τους. Ο διορθωμένος κώδικας δηλώνει τις συναρτήσεις  //getWidth// και //getHeight// ως //const// και έχει ως εξής: 
- 
-<code cpp Rectangle.cpp> 
-#include <iostream> 
-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::Rectangle(int width, int height) { 
-  this->width = width; this->height = height; 
-} 
- 
-void Rectangle::setWidth(int width) { this->width = width; } 
-void Rectangle::setHeight(int height) { this->height = height; } 
-int Rectangle::getWidth() const { return width; } 
-int Rectangle::getHeight() const { return height; } 
- 
-int main() { 
-  const Rectangle rect(10,5); 
-  cout << rect.getWidth(); 
-} 
-</code> 
- 
-===== Υπερφόρτωση const και non-const συναρτήσεων ===== 
- 
-Στον παραπάνω κώδικα μπορείτε να έχετε δύο εκδόσεις για τις συναρτήσεις getWidth() και getHeight() μία που εφαρμόζεται σε const και μία που εφαρμόζεται σε non-const αντικείμενα ως εξής: 
- 
-<code cpp Rectangle.cpp> 
-#include <iostream> 
-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::Rectangle(int width, int height) { 
-  this->width = width; this->height = height; 
-} 
- 
-void Rectangle::setWidth(int width) { this->width = width; } 
-void Rectangle::setHeight(int height) { this->height = height; } 
-int Rectangle::getWidth() const { cout << "I am the const method\n"; return width; } 
-int Rectangle::getHeight() const { return height; } 
-int Rectangle::getWidth() { cout << "I am the non-const method\n"; return width; } 
-int Rectangle::getHeight() { return height; } 
- 
-int main() { 
-  const Rectangle rect1(10,5); 
-  Rectangle rect2(10,5); 
-  cout << "rect1 width: " << rect1.getWidth() << endl; 
-  cout << "rect2 width: " << rect2.getWidth() << endl; 
-} 
-</code> 
- 
-<WRAP center round tip 60%> 
-Η ύπαρξη της //non-const// μεθόδου στο συγκεκριμένο παράδειγμα είναι πλεονασμός καθώς η //const// μέθοδος μπορεί να κληθεί και από //non-const// αντικείμενα. Όπως δείξαμε ο περιορισμός ισχύει στο αντίστροφο, δηλαδή ένα //const// αντικείμενο δεν μπορεί να καλέσει μία //non-const// μέθοδο. 
-</WRAP> 
- 
- 
- 
  
cpp/const_member_functions.txt · Last modified: 2021/05/07 07:50 (external edit)