CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured