CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    copy constructor??

    Can anyone see what's wrong with this , the print in the Copy Constructor never appears on the output, should it be called when "=" is used in main






    #include <iostream.h>

    class SimpleCat
    {
    public:
    SimpleCat (int age, int weight);
    ~SimpleCat() {}
    SimpleCat operator = (SimpleCat&);
    int GetAge() { return itsAge; }
    int GetWeight() { return itsWeight; }
    public:
    int itsAge;
    int itsWeight;
    };

    SimpleCat::SimpleCat(int age, int weight)
    {
    itsAge = age;
    itsWeight = weight;
    }

    SimpleCat SimpleCat:perator= (SimpleCat& cat)
    {
    cout << "Copy constructor called\n";
    return SimpleCat(cat.itsAge, cat.itsWeight);
    }

    int main()
    {
    SimpleCat Frisky(5, 8);
    SimpleCat Cat = Frisky; //Call copy cnstr
    SimpleCat& rCat = Frisky; //Call copy cnstr

    cout << "Frisky is: ";
    cout << Frisky.GetAge() << " years old. \n";
    cout << "rCat is " << rCat.GetAge() << " years old. \n";
    cout << "And Frisky weighs: ";
    cout << Frisky.GetWeight() << " pounds. \n";
    cout << "And rCat weighs: ";
    cout << rCat.GetWeight() << " pounds. \n\n";

    //Change the references values and print original
    rCat.itsAge = 25;
    rCat.itsWeight = 99;

    cout << "Frisky is: ";
    cout << Frisky.GetAge() << " years old. \n";
    cout << "rCat is " << rCat.GetAge() << " years old. \n";
    cout << "And Frisky weighs: ";
    cout << Frisky.GetWeight() << " pounds. \n";
    cout << "And rCat weighs: ";
    cout << rCat.GetWeight() << " pounds. \n";



    return 0;
    }

  2. #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.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: copy constructor??

    Here are the rules:
    Code:
    class CFoo  
    {
    public:
       CFoo() {cout << "default constructor\n";}
       CFoo(const CFoo& rFoo) {cout << "copy constructor\n";}
       CFoo& operator= (const CFoo& rFoo)  {cout << "assignment operator\n";};
    };
    
    CFoo a; // default constructor
    CFoo b(a); // copy construtor
    CFoo c = a; // copy construtor
    CFoo d(); // function declaration
    d = c; // assigment operator
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    Thumbs up Re: copy constructor??

    Thanks alls well now.

  5. #5
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: copy constructor??

    FYI:
    <iostream.h> is not part of the C++ standard, and should not be used.
    It will not compile on more compliant compilers like VC++ 7.x

    You should use <iostream> extensionless version instead, which is part of the C++ standard.
    #include <iostream>
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: copy constructor??

    More about what Axeter said in this FAQ.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: copy constructor??

    Quote Originally Posted by cilu
    More about what Axeter said in this FAQ.
    The above FAQ is a little misleading.

    Q: Could I still use the traditional standard header files?

    A: Almost all compilers, even those complying with the ISO standard, allow the use of the traditional header files (like 'iostream.h', 'stdlib.h', ...). Nevertheless, they were completely redesigned. All functions, classes and variables are declared under the namespace 'std' (This is not true for some header files supported by the VC++ 6.0 compiler which is known for not being very compliant to the C++ standard).



    This is categorizing <iostream.h> in the same category with <stdlib.h>

    <iostream.h> is not part of the official C++ standard, and never was part of the official C++ standard.
    <iostream.h> will not work on modern compliant compilers like VC++ 7.1

    <stdlib.h> is part of the official C++ standard. It's currently depricated, which means it's possible that some day it will not be part of the C++ standard.
    <stdlib.h> currently works on all C++ compliant compilers, to include modern compilers like VC++ 7.x

    The old ANSI C header files are still part of the official C++ standard with the *.h extension.
    All the STL library headers have no extension, and never had *.h extension within the OFFICIAL C++ standard.

    I still use stdlib.h in my code, especially when I think I might want to port the code to a C application.
    There is no real good reason to use <iostream.h>, unless you're using an obsolete compiler like Turbo C++.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

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