// functionTwo, passes a const pointer
const SimpleCat * const FunctionTwo (const SimpleCat * const theCat)
{
cout << "Function Two. Returning...\n";
cout << "Frisky is now " << theCat->GetAge();
cout << " years old \n";
// theCat->SetAge(8); const!
return theCat;
}
Okay const is a command that does not let a value get changed right? And pointers point to the memory location of a value and can also change the value as well right? If both of these definitions are true , then this function (FunctionTwo), is a class of a SimpleCat that is assigning the memory address to FunctionTwo in which both of their values stay the same. Also the parameter in the function is an object of the class SimpleCat that has become a pointer and they both cannot change values. So finally in the function, theCat object is pointing to the memory address of GetAge which value is 5. So now theCat object has the value 5 stored within itself and is returned ... This is my summary of what I think is happening can someone please tell me if what I'm saying is correct or wrong
Also here's the rest of the code so you can get an idea of it
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);

New Topic/Question
Reply



MultiQuote






|