This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | Previous revision | ||
|
cpp:this [2020/04/08 14:32] |
cpp:this [2020/04/08 15:32] gthanos |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Ο δείκτης this ====== | ||
| + | |||
| + | Κατά τον ορισμό συναρτήσεων της κλάσης θέλουμε συχνά να αναφερθούμε στο αντικείμενο καλεί τη συγκεκριμένη μέθοδο. Ο τελεστής χρησιμοποιείται //α)// κατά την [[cpp: | ||
| + | |||
| + | Το παρακάτω παράδειγμα της κλάσης Rectangle αναφέρεται στη δεύτερη περίπτωση, | ||
| + | |||
| + | <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:: | ||
| + | </ | ||
| + | |||
| + | <code cpp RectangleUsage.cpp> | ||
| + | #include " | ||
| + | |||
| + | int main () { | ||
| + | Rectangle rect(5,6); | ||
| + | cout << "area: " << rect.getWidth() * rect.getHeight() << endl; | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP center round todo 80%> | ||
| + | Αφαιρέστε τον δείκτη //this// από το παραπάνω παράδειγμα, | ||
| + | </ | ||
| + | |||