Advertisement

rhs and the Copy Constructor

Started by February 25, 2000 06:51 AM
0 comments, last by Fredric 25 years ago
As many of you know, I''m learning C++. As many of you also know, the book is full of little things that he doesn''t explain, and expects the rocket-scientist reader to understand without haven been given an explanation. It has happened again! Read the following piece of code, and I''ll ask my question at the bottom... #include ''iostream.h'' class CAT { public: CAT(); CAT (const CAT &); // copy constructor ~CAT(); // destructor int GetAge() const { return *itsAge; } int GetWeight() const { return *itsWeight; } void SetAge(int age) { *itsAge = age; } private: int * itsAge; int *itsWeight; }; CAT::CAT() { itsAge = new int; if (itsAge == NULL) { cout << "Error! Not enough memory to execute itsAge on the heap!\n"; } *itsAge = 5; itsWeight = new int; if (itsWeight == NULL) { cout << "Error! Not enough memory to execute itsWeight on the heap!\n"; } *itsWeight = 9; } CAT::CAT(const CAT & rhs) // copy constructor { itsAge = new int; if (itsAge == NULL) { cout << "Error! Not enough memory to execute itsAge on the heap!\n"; } *itsAge = rhs.GetAge(); itsWeight = new int; if (itsWeight == NULL) { cout << "Error! Not enough memory to execute itsWeight on the heap!\n"; } *itsWeight = rhs.GetWeight(); } CAT::~CAT() { delete itsAge; itsAge = 0; delete itsWeight; itsWeight = 0; } int main() { CAT Frisky; cout << "Frisky''s age: " << Frisky.GetAge() << endl; cout << "Setting Frisky''s age to 6...\n"; Frisky.SetAge(6); cout << "Frisky has made Tom!\n"; CAT Tom(Frisky); cout << "Frisky''s age: " << Frisky.GetAge() << "\n"; cout << "Tom''s age: " << Tom.GetAge() << endl; cout << "Setting Frisky to 7...\n"; Frisky.SetAge(7); cout << "Frisky''s age: " << Frisky.GetAge() << endl; cout << "Tom''s age: " << Tom.GetAge() << endl; return 0; } This code is all fine and dandy! No problems until I get to the copy constructor! CAT::CAT(const CAT & rhs) <-- what the heck is rhs? when was that declared and initialized? that''s like, a phantom ''something'' to me! This is kicking me... the author in the book simply says that rhs stands for right hands side. So what?!?! When was rhs declared, and why is it so important? *sigh* next time I get some money, I''m buying a new C++ book.... Thanks everyone, in advance, if you try tom solve my problem. I know I must be getting annoying posting so many questions, but really- blame it on the author! hehe Programming::~Fredric(const Annoy_Ance)
3D Math- The type of mathematics that'll put hair on your chest!
The rhs you are pointing at is a parameter, and as such is not declared anywhere. When the function is called, and instantiated object has to be passed as a parameter value, and this happens on the following line:

CAT Tom(Frisky);

Here, Tom''s CAT::CAT(const CAT & rhs) is called, with rhs equal to Frisky. In the copy constructor method it is used as the source of the data which is used to set the parameters of the object being created (in this case, Tom).

Personally, I have never used copy constructors for the purposes they are meant for. In the example you provided, a better way of thinking would be to simply picture the function as a constructor with input parameters. There are some cases where they are useful, the most common example being a class with a pointer to a buffer. If you create another object of the same class with the same values as the original, you might want to copy the contents of the buffer, not just the pointer.

This topic is closed to new replies.

Advertisement