This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | Previous revision | ||
|
cpp:this [2019/03/07 05:34] gthanos |
cpp:this [2020/04/08 14:32] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Ο δείκτης this ====== | ||
| - | |||
| - | Κατά τον ορισμό συναρτήσεων της κλάσης θέλουμε συχνά να αναφερθούμε στο αντικείμενο καλεί τη συγκεκριμένη μέθοδο. Ο τελεστής μπορεί να έχει διάφορες χρήσεις, | ||
| - | |||
| - | <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// από το παραπάνω παράδειγμα, | ||
| - | </ | ||
| - | |||