CAT *Cat1 = new CAT;
cout << "Cat1's age: " << Cat1->GetAge() << endl;
cout << "Setting Cat1's age to 6..." << endl;
CAT *Cat2 = new CAT;
cout << "Cat2's age: " << Cat2->GetAge() << endl;
cout << "Now setting Cat2 equal to Cat1... \n";
Cat2 = Cat1;
cout << "Cat2's age now: " << Cat2->GetAge() << endl;
I have a simple question: why does the overloaded assignment operator return a reference to a CAT object rather than a CAT object? Does it make any difference if you change it to return a CAT object instead? I tried changing it and couldn't see any difference in the output; after all all it should do is make a "shallow copy" of Cat1 and point it to the same area in the free store.
Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.