|
-
September 6th, 2005, 06:27 AM
#2
Re: copy constructor??
Operator '=' is different from the copy constructor. It is called the copy assignment operator and is called when you use this operator with two fully build compatible objects. And the call to the copy constructor is being made with the implicit version generated by the compiler. Here is your code with some corrections:
Code:
#include <iostream>
class SimpleCat
{
public:
//this is the copy assignment/overloaded assignment operator
SimpleCat& operator=(const SimpleCat& cat);
//constructor with 2 arguments
SimpleCat (int age, int weight):itsAge(age),itsWeight(weight) {
std::cout << "Constructor with 2 argument called.\n\n";
}
//this is the copy constructor
SimpleCat::SimpleCat(const SimpleCat& object):itsAge(object.GetAge()),itsWeight(object.GetAge()) {
std::cout << "Copy constructor called.\n\n";
}
~SimpleCat() { }
int GetAge() const { return itsAge; } //declare the getters as const
int GetWeight() const { return itsWeight; } //declare the getters as const
void SetAge(int age){ itsAge=age; }
void SetWeight(int weight) { itsWeight=weight; }
private: // i think it would be private and that is what you must have intended instead of "public:"
int itsAge;
int itsWeight;
};
SimpleCat& SimpleCat::operator=(const SimpleCat& rhs) //note the difference in argument and return type
{
if (this!=&rhs) { // make sure both are not the same object
itsAge=rhs.GetAge();
itsWeight=rhs.GetWeight();
}
std::cout << "Copy assignment operator called.\n";
return *this; // Return *this to acoomodate for multiple assignment
}
int main()
{
SimpleCat Frisky(5, 8);
SimpleCat Cat = Frisky; //Call copy cnstr
SimpleCat& rCat = Frisky; //This is not a call to the copy constructor - its just making another reference to the same object
std::cout << "Frisky is: " << Frisky.GetAge() << " years old. \n";
std::cout << "rCat is " << rCat.GetAge() << " years old. \n";
std::cout << "And Frisky weighs: " << Frisky.GetWeight() << " pounds. \n";
std::cout << "And rCat weighs: " << rCat.GetWeight() << " pounds. \n\n";
//Change the references values and print original
rCat.SetAge(25);
rCat.SetWeight(99);
std::cout << "Changed rCat's age to 25 and weight to 99.\n\n";
std::cout << "Frisky is: " << Frisky.GetAge() << " years old. \n";
std::cout << "rCat is " << rCat.GetAge() << " years old. \n";
std::cout << "And Frisky weighs: " << Frisky.GetWeight() << " pounds. \n";
std::cout << "And rCat weighs: " << rCat.GetWeight() << " pounds. \n";
return 0;
}
Also, you should note that:
Code:
SimpleCat& rCat = Frisky; //Call copy cnstr
this is not a call to the copy constructor. It just creates a reference by the name rCat for the same object Frisky. Hope this helps. Regards. 
EDIT : And please use CODE tags to keep your code separated out from the rest of the post like I have done above. Thanks.
Last edited by exterminator; September 6th, 2005 at 07:38 AM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|