Code:
1: //Listing 9.11
2: // Passing pointers to objects
3:
4: #include <iostream.h>
5:
6: class SimpleCat
7: {
8: public:
9: SimpleCat();
10: SimpleCat(SimpleCat&);
11: ~SimpleCat();
12:
13: int GetAge() const { return itsAge; }
14: void SetAge(int age) { itsAge = age; }
15:
16: private:
17: int itsAge;
18: };
19:
20: SimpleCat::SimpleCat()
21: {
22: cout << "Simple Cat Constructor...\n";
23: itsAge = 1;
24: }
25:
26: SimpleCat::SimpleCat(SimpleCat&)
27: {
28: cout << "Simple Cat Copy Constructor...\n";
29: }
30:
31: SimpleCat::~SimpleCat()
32: {
33: cout << "Simple Cat Destructor...\n";
34: }
35:
36:const SimpleCat * const FunctionTwo (const SimpleCat * const theCat);
37:
38: int main()
39: {
40: cout << "Making a cat...\n";
41: SimpleCat Frisky;
42: cout << "Frisky is " ;
43 cout << Frisky.GetAge();
44: cout << " years _old\n";
45: int age = 5;
46: Frisky.SetAge(age);
47: cout << "Frisky is " ;
48 cout << Frisky.GetAge();
49: cout << " years _old\n";
50: cout << "Calling FunctionTwo...\n";
51: FunctionTwo(&Frisky);
52: cout << "Frisky is " ;
53 cout << Frisky.GetAge();
54: cout << " years _old\n";
55: return 0;
56: }
57:
58: // functionTwo, passes a const pointer
59: const SimpleCat * const FunctionTwo (const SimpleCat * const theCat)
60: {
61: cout << "Function Two. Returning...\n";
62: cout << "Frisky is now " << theCat->GetAge();
63: cout << " years old \n";
64: // theCat->SetAge(8); const!
65: return theCat;
66: }
Output: Making a cat...
Simple Cat constructor...
Frisky is 1 years old
Frisky is 5 years old
Calling FunctionTwo...
FunctionTwo. Returning...
Frisky is now 5 years old
Frisky is 5 years old
Simple Cat Destructor...
1: //Listing 9.11
2: // Passing pointers to objects
3:
4: #include <iostream.h>
5:
6: class SimpleCat
7: {
8: public:
9: SimpleCat();
10: SimpleCat(SimpleCat&);
11: ~SimpleCat();
12:
13: int GetAge() const { return itsAge; }
14: void SetAge(int age) { itsAge = age; }
15:
16: private:
17: int itsAge;
18: };
19:
20: SimpleCat::SimpleCat()
21: {
22: cout << "Simple Cat Constructor...\n";
23: itsAge = 1;
24: }
25:
26: SimpleCat::SimpleCat(SimpleCat&)
27: {
28: cout << "Simple Cat Copy Constructor...\n";
29: }
30:
31: SimpleCat::~SimpleCat()
32: {
33: cout << "Simple Cat Destructor...\n";
34: }
35:
36:const SimpleCat * const FunctionTwo (const SimpleCat * const theCat);
37:
38: int main()
39: {
40: cout << "Making a cat...\n";
41: SimpleCat Frisky;
42: cout << "Frisky is " ;
43 cout << Frisky.GetAge();
44: cout << " years _old\n";
45: int age = 5;
46: Frisky.SetAge(age);
47: cout << "Frisky is " ;
48 cout << Frisky.GetAge();
49: cout << " years _old\n";
50: cout << "Calling FunctionTwo...\n";
51: FunctionTwo(&Frisky);
52: cout << "Frisky is " ;
53 cout << Frisky.GetAge();
54: cout << " years _old\n";
55: return 0;
56: }
57:
58: // functionTwo, passes a const pointer
59: const SimpleCat * const FunctionTwo (const SimpleCat * const theCat)
60: {
61: cout << "Function Two. Returning...\n";
62: cout << "Frisky is now " << theCat->GetAge();
63: cout << " years old \n";
64: // theCat->SetAge(8); const!
65: return theCat;
66: }
Output: Making a cat...
Simple Cat constructor...
Frisky is 1 years old
Frisky is 5 years old
Calling FunctionTwo...
FunctionTwo. Returning...
Frisky is now 5 years old
Frisky is 5 years old
Simple Cat Destructor...
1) Deklaracija/definicija FunctionTwo kaze da ta f-ja vraca const pokazivac na const objekat klase SimpleCat.
2) Arg. te f-je je takodje pokazivac (*const theCat)
Return ove f-je, medjutim, vraca theCat (sto je, u stvari samo adresa objekta, a ne pokazivac kako kaze deklaracija).
Zasto u returnu nije *theCat?
After all, ova f-ja nista ni ne vraca, nego samo stampa neki rezultat. Pa da li je onda moguce da se izostavi return ?
Dodatno pitanje bi bilo i: zasto operator za indirekciju (->), kada objekat nije kreiran na slobodnom skladistu (new)? - obzirom na :
Citat:
You accessed data members and functions by using the dot (.) operator for Cat objects created locally. To access the Cat object on the free store, you must dereference the pointer and call the dot operator on the object pointed to by the pointer. Therefore, to access the GetAge member function, you would write
(*pRags).GetAge();
Parentheses are used to assure that pRags is dereferenced before GetAge() is accessed.
Because this is cumbersome, C++ provides a shorthand operator for indirect access: the points-to operator (->)
(*pRags).GetAge();
Parentheses are used to assure that pRags is dereferenced before GetAge() is accessed.
Because this is cumbersome, C++ provides a shorthand operator for indirect access: the points-to operator (->)
Hvala na eventualnim odgovorima.