Αναφορές (References) Σήμερα! Αναφορές (references) Που ορίζουμε αναφορά Αναφορές σε αντικείμενα Πέρασμα με αναφορά Πέρασμα πολλαπλών τιμών 2 1
Αναφορές (references) Οι αναφορές έχουν τη δύναμη των δεικτών αλλά με πιο εύκολη σύνταξη Όταν δημιουργούμε μια αναφορά την αρχικοποιούμε με μία άλλη μεταβλητή (στόχο) int &rsomeref = someint; Η αναφορά άλειτουργεί ως εναλλακτικό όνομα για το στόχο και ότι κάνουμε στην αναφορά περνάει στο στόχο 3 Παράδειγμα int main() int intone; int &rsomeref = intone; intone = 5; cout << ʺintOne: ʺ << intone << endl; cout << ʺrSomeRef: ʺ << rsomeref << endl; rsomeref = 7; cout << ʺintOne: ʺ << intone << endl; cout << ʺrSomeRef: ʺ << rsomeref << endl; intone: 5 rsomeref: 5 intone: 7 rsomeref: 7 4 2
Παράδειγμα int main() int intone; int &rsomeref = intone; intone = 5; cout << ʺintOne: ʺ << intone << endl; cout << ʺrSomeRef: ʺ << rsomeref << endl; cout << ʺ&intOne: ʺ << &intone << endl; cout << ʺ&rSomeRef: ʺ << &rsomeref << endl; intone: 5 rsomeref: 5 &intone: 0x3500 5 &rsomeref: 0x3500 Παράδειγμα/1 int main() int intone; int &rsomeref = intone; intone = 5; cout << ʺintOne:\tʺ << intone << endl; cout << ʺrSomeRef:\tʺ << rsomeref << endl; cout << ʺ&intOne:\tʺ << &intone << endl; cout << ʺ&rSomeRef:\tʺ << &rsomeref << endl; int inttwo = 8; 6 3
Παράδειγμα/2 rsomeref = inttwo; cout << ʺ\nintOne:\tʺ << intone << endl; cout << ʺintTwo:\tʺ << inttwo << endl; cout << ʺrSomeRef:\tʺ << rsomeref << endl; cout << ʺ&intOne:\tʺ << &intone << endl; cout << ʺ&intTwo:\tʺ << &inttwo << endl; cout << ʺ&rSomeRef:\tʺ << &rsomeref; intone: 5 rsomeref: 5 &intone: 0x213e &rsomeref: 0x213e intone: 8 inttwo: 8 rsomeref: 8 &intone: 0x213e &inttwo: 0x2130 7 &rsomeref: 0x213e Συμβουλές Αρχικοποιείται όλες τις αναφορές σε στόχους ΜΗΝ προσπαθήστε να ορίσετε νέο στόχο σε αναφορά Μην συγχέετε τον τελεστή διεύθυνσης με τον τελεστή δήλωσης αναφοράς 8 4
Που ορίζουμε αναφορά int t& rintref = int; Λάθος Σωστό int howbig = 200; int & rintref = howbig; 9 Που ορίζουμε αναφορά Μπορούμε να ορίσουμε αναφορά σε αντικείμενο Δεν μπορούμε να ορίσουμε αναφορά σε κλάση Οι αναφορές πρέπει να αρχικοποιούνται κατά τη δήλωση int hisage; int &rage = hisage; CAT boots; CAT &rcatref = boots; 10 5
Που ορίζουμε αναφορά Λάθος CAT & rcatref = CAT; Σωστό CAT frisky; CAT & rcatref = frisky; 11 Αναφορές σε αντικείμενα/1 class SimpleCat public: SimpleCat (int age, int weight); ~SimpleCat() int GetAge() return itsage; int GetWeight() return itsweight; private: int itsage; int itsweight; ; 12 6
Αναφορές σε αντικείμενα/2 SimpleCat::SimpleCat(int age, int weight) itsage = age; g; itsweight = weight; int main() SimpleCat Frisky(5,8); SimpleCat & rcat = Frisky; cout << ʺFrisky is: ʺ; cout << Frisky.GetAge() << ʺ years old. \nʺ; cout << ʺAnd Frisky weighs: ʺ; cout << rcat.getweight() << ʺ pounds. \nʺ; Frisky is: 5 years old. 13 And Frisky weighs 8 pounds. Πέρασμα με αναφορά Το πέρασμα με αναφορά μπορεί να γίνει με τη χρήση δεικτών ή αναφορών Σε αυτή την περίπτωση αντί να περάσουμε αντίγραφο των δεδομένων στη συνάρτηση, περνάμε τα original δεδομένα. 14 7
Πέρασμα με τιμή/1 void swap(int x, int y); y; int main() int x = 5, y = 10; cout << ʺMain. Before swap, x: ʺ << x << ʺ y: ʺ << y << ʺ\nʺ; swap(x,y); cout << ʺMain. After swap, x: ʺ << x << ʺ y: ʺ << y << ʺ\nʺ; 15 Πέρασμα με τιμή/2 void swap (int x, int y) int temp; cout << ʺSwap. Before swap, x: ʺ << x << ʺ y: ʺ << y << ʺ\nʺ; temp = x; x = y; y = temp; cout << ʺSwap. After swap, x: ʺ << x << ʺ y: ʺ << y << ʺ\nʺ; Main. Before swap, x: 5 y: 10 Swap. Before swap, x: 5 y: 10 Swap. After swap, x: 10 y: 5 16 Main. After swap, x: 5 y: 10 8
Πέρασμα με αναφορά (δείκτες)/1 void swap(int *x, int *y); y; int main() int x = 5, y = 10; cout << ʺMain. Before swap, x: ʺ << x << ʺ y: ʺ << y << ʺ\nʺ; swap(&x,&y); cout << ʺMain. After swap, x: ʺ << x << ʺ y: ʺ << y << ʺ\nʺ; 17 Πέρασμα με αναφορά (δείκτες)/2 void swap (int *px, int *py) int temp; cout << ʺSwap. Before swap, *px: ʺ << *px << ʺ *py: ʺ << *py << ʺ\nʺ; temp = *px; *px = *py; *py = temp; cout << ʺSwap. After swap, *px: ʺ << *px << ʺ *py: ʺ << *py << ʺ\nʺ; Main. Before swap, x: 5 y: 10 Swap. Before swap, *px: 5 *py: 10 Swap. After swap, *px: 10 *py: 185 Main. After swap, x: 10 y: 5 9
Πέρασμα με αναφορά (αναφορές)/1 void swap(int &rx, int &ry); y; int main() int x = 5, y = 10; cout << ʺMain. Before swap, x: ʺ << x << ʺ y: ʺ << y << ʺ\nʺ; swap(x,y); cout << ʺMain. After swap, x: ʺ << x << ʺ y: ʺ << y << ʺ\nʺ; 19 Πέρασμα με αναφορά (αναφορές)/2 void swap (int &rx, int &ry) int temp; cout << ʺSwap. Before swap, rx: ʺ << rx << ʺ ry: ʺ << ry << ʺ\nʺ; temp = rx; rx = ry; ry = temp; cout << ʺSwap. After swap, rx: ʺ << rx << ʺ ry: ʺ << ry << ʺ\nʺ; 20 10
Πέρασμα πολλαπλών τιμών με δείκτες/1 typedef unsigned short USHORT; short Factor(USHORT, USHORT*, USHORT*); int main() USHORT number, squared, cubed; short error; cout << ʺEnter a number (0 20): ʺ; cin >> number; error = Factor(number, &squared, &cubed); if (!error) cout << ʺnumber: ʺ << number << ʺ\nʺ; cout << ʺsquare: ʺ << squared << ʺ\nʺ; cout << ʺcubed: ʺ << cubed << ʺ\nʺ; else cout << ʺError encountered!!\nʺ; 21 Πέρασμα πολλαπλών τιμών με δείκτες/2 short Factor(USHORT n, USHORT *psquared, USHORT *pcubed) short Value = 0; if (n > 20) Value = 1; else *psquared = n*n; *pcubed = n*n*n; Value = 0; return Value; Enter a number (0-20): 3 number: 3 square: 9 cubed: 27 22 11
Πέρασμα πολλαπλών τιμών με αναφορές/1 typedef unsigned short USHORT; enum ERR_CODE SUCCESS, ERROR ; ERR_CODE Factor(USHORT, USHORT&, USHORT&); int main() USHORT number, squared, cubed; ERR_CODE result; cout << ʺEnter a number (0 20): ʺ; cin >> number; result = Factor(number, squared, cubed); if (result == SUCCESS) cout << ʺnumber: ʺ << number << ʺ\nʺ; cout << ʺsquare: ʺ << squared << ʺ\nʺ; cout << ʺcubed: ʺ << cubed << ʺ\nʺ; else cout << ʺError encountered!!\nʺ; return 0 23 Πέρασμα πολλαπλών τιμών με αναφορές/2 ERR_CODE Factor(USHORT n, USHORT &rsquared, USHORT &rcubed) if (n > 20) return ERROR; else rsquared = n*n; rcubed = n*n*n; return SUCCESS; 24 12
Αναφορές σε αντικείμενα/1 class SimpleCat public: SimpleCat(); SimpleCat(SimpleCat&); ~SimpleCat(); int GetAge() const return itsage; void SetAge(int age) itsage = age; private: int itsage; ; 25 Αναφορές σε αντικείμενα/2 SimpleCat::SimpleCat() cout << ʺSimple p Cat Constructor...\nʺ; itsage = 1; SimpleCat::SimpleCat(SimpleCat&) cout << ʺSimple Cat Copy Constructor...\nʺ; SimpleCat::~SimpleCat() cout << ʺSimple Cat Destructor...\nʺ; const SimpleCat & FunctionTwo (const SimpleCat & thecat); 26 13
Αναφορές σε αντικείμενα/3 int main() cout << ʺMaking g a cat...\nʺ; SimpleCat Frisky; cout << ʺFrisky is ʺ << Frisky.GetAge() << ʺ years old\nʺ; int age = 5; Frisky.SetAge(age); cout << ʺFrisky is ʺ << Frisky.GetAge() << ʺ years old\nʺ; cout << ʺCalling FunctionTwo...\nʺ; FunctionTwo(Frisky); cout << ʺFrisky is ʺ << Frisky.GetAge() << ʺ years old\nʺ; 27 Αναφορές σε αντικείμενα/4 const SimpleCat & FunctionTwo (const SimpleCat & thecat) cout << ʺFunction Two. Returning...\nʺ; cout << ʺFrisky is now ʺ << thecat.getage(); cout << ʺ years old \nʺ; return thecat; 28 14
??? class CAT public: CAT(int age) itsage = age; ~CAT() int GetAge() const return itsage; private: int itsage; ; CAT & MakeCat(int age); int main() int age = 7; CAT Boots = MakeCat(age); cout << ʺBoots is ʺ << Boots.GetAge() << ʺ years old\nʺ; CAT & MakeCat(int age) CAT * pcat = new CAT(age); return *pcat; 29 15