cpp:const_member_functions
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| cpp:const_member_functions [2017/05/15 07:18] – 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 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> | <code cpp Rectangle.cpp> | ||
| #include < | #include < | ||
| - | #include < | ||
| - | #include < | ||
| using namespace std; | using namespace std; | ||
| - | |||
| - | #include " | ||
| class Rectangle { | class Rectangle { | ||
| private: | private: | ||
| int width, height; | int width, height; | ||
| - | Point *origin; | ||
| public: | public: | ||
| - | Rectangle(int | + | Rectangle(int |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | Point & | + | |
| }; | }; | ||
| - | Rectangle:: | + | Rectangle:: |
| - | width = w; height = h; | + | |
| - | origin = new (nothrow) Point( p.getX(), p.getY() ); | + | |
| - | if(origin == NULL) { | + | |
| - | cerr << " | + | |
| - | exit(-1); | + | |
| - | } | + | |
| } | } | ||
| - | Rectangle::~Rectangle() { | + | void Rectangle::setWidth(int width) { this-> |
| - | delete origin; | + | void Rectangle::setHeight(int height) { this-> |
| - | } | + | int Rectangle:: |
| - | + | int Rectangle::getHeight() { return | |
| - | void Rectangle::setOrigin(Point &p) { | + | |
| - | if(origin!=NULL) | + | |
| - | delete origin; | + | |
| - | + | ||
| - | origin = new (nothrow) Point( p.getX(), p.getY() ); | + | |
| - | if(origin == NULL) { | + | |
| - | cerr << " | + | |
| - | exit(-1); | + | |
| - | | + | |
| - | } | + | |
| - | + | ||
| - | Point &Rectangle::getOrigin() { return | + | |
| int main() { | int main() { | ||
| - | | + | const Rectangle rect(10,5); |
| - | | + | |
| } | } | ||
| </ | </ | ||
| - | Στον παραπάνω κώδικα επιχειρήστε να διαβάσετε το πεδίο | + | Στον παραπάνω κώδικα επιχειρήστε να διαβάσετε την μεταβλητή |
| <code cpp> | <code cpp> | ||
| int main() { | int main() { | ||
| - | | + | const Rectangle rect(10,5); |
| - | | + | |
| - | | + | |
| } | } | ||
| </ | </ | ||
| - | Σε αυτή την περίπτωση λαμβάνετε το παρακάτω μήνυμα λάθους: | + | Σε αυτή την περίπτωση λαμβάνετε το παρακάτω μήνυμα λάθους |
| < | < | ||
| - | Rectangle.cpp: In function ‘int main()’: | + | Rectangle5.cpp: In function ‘int main()’: |
| - | Rectangle.cpp:46:35: error: passing ‘const Rectangle’ as ‘this’ argument of ‘Point& | + | Rectangle5.cpp:26:25: error: passing ‘const Rectangle’ as ‘this’ argument of ‘int Rectangle::getWidth()’ discards qualifiers [-fpermissive] |
| - | const Point p1 = rect.getOrigin(); | + | cout << |
| - | | + | |
| </ | </ | ||
| - | Η επεξήγηση του παραπάνω μηνύματος είναι ότι εφόσον το αντικείμενο είναι //const// θα πρέπει και | + | Η επεξήγηση του παραπάνω μηνύματος είναι ότι εφόσον το αντικείμενο είναι //const// θα πρέπει και |
| - | <code cpp> | + | < |
| - | Point & | + | # |
| - | </code> | + | 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() const; | ||
| + | }; | ||
| - | Στον παραπάνω κώδικα αν και η συνάρτηση // | + | Rectangle:: |
| + | this-> | ||
| + | } | ||
| + | |||
| + | void Rectangle:: | ||
| + | void Rectangle:: | ||
| + | int Rectangle:: | ||
| + | int Rectangle::getHeight() const { return height; } | ||
| - | <code cpp> | ||
| int main() { | int main() { | ||
| - | | + | const Rectangle rect(10,5); |
| - | | + | |
| - | | + | |
| - | p1.setX(p1.getX()+1); | + | |
| } | } | ||
| - | < | + | </code> |
| + | |||
| + | ===== Υπερφόρτωση const και non-const συναρτήσεων ===== | ||
| - | Για | + | Στον παραπάνω |
| <code cpp Rectangle.cpp> | <code cpp Rectangle.cpp> | ||
| #include < | #include < | ||
| - | #include < | ||
| - | #include < | ||
| using namespace std; | using namespace std; | ||
| - | |||
| - | #include " | ||
| class Rectangle { | class Rectangle { | ||
| private: | private: | ||
| int width, height; | int width, height; | ||
| - | Point *origin; | ||
| public: | public: | ||
| - | Rectangle(int | + | Rectangle(int |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| + | int getHeight(); | ||
| }; | }; | ||
| - | Rectangle:: | + | Rectangle:: |
| - | width = w; height = h; | + | |
| - | origin = new (nothrow) Point( p.getX(), p.getY() ); | + | |
| - | if(origin == NULL) { | + | |
| - | cerr << " | + | |
| - | exit(-1); | + | |
| - | } | + | |
| } | } | ||
| - | Rectangle::~Rectangle() { | + | void Rectangle::setWidth(int width) { this-> |
| - | | + | void Rectangle::setHeight(int height) { this-> |
| - | } | + | int Rectangle:: |
| + | int Rectangle:: | ||
| + | int Rectangle:: | ||
| + | int Rectangle:: | ||
| - | void Rectangle:: | + | int main() { |
| - | | + | |
| - | delete origin; | + | |
| - | | + | |
| - | origin = new (nothrow) Point( p.getX(), p.getY() | + | |
| - | | + | |
| - | | + | |
| - | exit(-1); | + | |
| - | } | + | |
| } | } | ||
| + | </ | ||
| + | |||
| + | <WRAP center round tip 80%> | ||
| + | Στο παραπάνω παράδειγμα, | ||
| + | </ | ||
| + | |||
| - | 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(); | ||
| - | } | ||
| - | </ | ||
| - | < | ||
cpp/const_member_functions.1494832721.txt.gz · Last modified: 2017/05/15 06:18 (external edit)
