User Tools

Site Tools


cpp:const_member_functions

This is an old revision of the document!


Const μέθοδοι της κλάσης

Όταν δηλώνεται ένα αντικείμενο ως const (όπως παρακάτω), τότε οι μεταβλητές του αντικειμένου μπορούν μόνο να διαβαστούν αλλά όχι να μεταβληθούν, πρόκειται δηλαδή για ένα αμετάβλητο αντικείμενο. Παρατηρήστε βέβαια, ότι ο κατασκευαστής του αντικειμένου αρχικοποιεί κανονικά το αντικείμενο και μεταβάλλει τις μεταβλητές του.

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);  
}

Στον παραπάνω κώδικα επιχειρήστε να διαβάσετε την μεταβλητή width, ως εξής:

int main() {
  const Rectangle rect(10,5);
  cout << rect.getWidth();
}

Σε αυτή την περίπτωση λαμβάνετε το παρακάτω μήνυμα λάθους:

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();                                                                                                                                                                                        
                         ^

Η επεξήγηση του παραπάνω μηνύματος είναι ότι εφόσον το αντικείμενο είναι const θα πρέπει και η μέθοδοι που χρησιμοποιούμε για να προσπελάσουμε το αντικείμενο να είναι const, δηλαδή να δηλώνουν ότι δεν μεταβάλλουν το αντικείμενο κατά την εκτέλεση τους. Ο διορθωμένος κώδικας δηλώνει τις συναρτήσεις getWidth και getHeight ως const και έχει ως εξής:

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();
}
cpp/const_member_functions.1494833587.txt.gz · Last modified: 2017/05/15 06:33 (external edit)