User Tools

Site Tools


cpp:this

Differences

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

Link to this comparison view

Next revision
Previous revision
cpp:this [2017/04/20 15:39]
gthanos created
cpp:this [2020/04/08 14:32]
Line 1: Line 1:
-====== Ο δείκτης this ====== 
- 
-Κατά τον ορισμό συναρτήσεων της κλάσης θέλουμε συχνά να αναφερθούμε στο αντικείμενο καλεί τη συγκεκριμένη μέθοδο. Ο τελεστής μπορεί να έχει διάφορες χρήσεις, χρησιμοποιείται κατά την υπερφόρτωση τελεστών καθώς και για να αποφευχθούν συγκρούσεις ονομάτων όταν τοπικές μεταβλητές και πεδία της κλάσης έχουν το ίδιο όνομα. Δείτε παρακάτω το παράδειγμα της κλάσης Rectangle, όπου ο κατασκευαστής της κλάσης και οι μέθοδοι της κλάσης λαμβάνουν ορίσματα που ταυτίζονται με τα ονόματα των πεδίων. 
- 
-<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; } 
-</code> 
- 
- 
  
cpp/this.txt · Last modified: 2020/04/08 14:32 (external edit)