Click to See Complete Forum and Search --> : copy constructor??
ireland
September 6th, 2005, 06:10 AM
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::operator= (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;
}
exterminator
September 6th, 2005, 06:27 AM
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:#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:
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. :thumb:
EDIT : And please use CODE tags to keep your code separated out from the rest of the post like I have done above. Thanks.
cilu
September 6th, 2005, 07:13 AM
Here are the rules:
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
ireland
September 6th, 2005, 08:13 AM
Thanks alls well now.
Axter
September 6th, 2005, 11:19 AM
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>
cilu
September 6th, 2005, 01:37 PM
More about what Axeter said in this FAQ.
Axter
September 6th, 2005, 02:02 PM
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++.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.