|
|
|
|
|
|
|
The solution is to pass a const pointer to SimpleCat. Doing so prevents calling any non-const method on SimpleCat, and thus protects the object from change. Listing 12.2 demonstrates this idea. |
|
|
|
|
|
|
|
|
LISTING 12.2 PASSING const POINTERS |
|
|
|
 |
|
|
|
|
1: //Listing 12.2
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
37: FunctionTwo (const SimpleCat * const theCat);
38:
39: int main()
40: {
41: cout << Making a cat\n;
42: SimpleCat Frisky;
43: cout << Frisky is ; |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|