K
Kelly Mandrake
I've implemented a class with operator+ overloaded and I also provided
my own copy constructor; The problem is my copy constructor is being
called twise and I dont understand why. I believe the copy constructor
is being called when I add the two class objects. But if this is so,
then why because I have overloaded the + operator should I not get the
result from that and not from copy constructor?
I have the following:
int main(int argc, char *argv[])
{
CVector a(2,5);
CVector b(3,6);
CVector c;
c = a + b;
cout << "X: " << c.GetX() << " Y: " << c.GetY() << endl;
DisplayCopy(c);
system("PAUSE");
return EXIT_SUCCESS;
}
CVector CVector:perator+(CVector param)
{
CVector temp;
temp.SetX( x + param.GetX() );
temp.SetY( y + param.GetY() );
return temp;
}
CVector::CVector(const CVector &rhs)
{
cout << "Inside Copy Constructor..." << endl;
x = rhs.x + 3;
y = rhs.y + 3;
}
void DisplayCopy(CVector copy)
{
cout << "X: " << copy.GetX() << " Y: " << copy.GetY() << endl;
}
==================================================
class CVector
{
public:
CVector(int valueX = 0, int valueY = 0): x(valueX), y(valueY) {}
CVector(const CVector &rhs); // Copy Constructor
CVector operator+(CVector param);
int GetX() const { return x; }
int GetY() const { return y; }
void SetX(int valueX) { x = valueX; }
void SetY(int valueY) { y = valueY; }
protected:
int x, y;
};
void DisplayCopy(CVector copy);
my own copy constructor; The problem is my copy constructor is being
called twise and I dont understand why. I believe the copy constructor
is being called when I add the two class objects. But if this is so,
then why because I have overloaded the + operator should I not get the
result from that and not from copy constructor?
I have the following:
int main(int argc, char *argv[])
{
CVector a(2,5);
CVector b(3,6);
CVector c;
c = a + b;
cout << "X: " << c.GetX() << " Y: " << c.GetY() << endl;
DisplayCopy(c);
system("PAUSE");
return EXIT_SUCCESS;
}
CVector CVector:perator+(CVector param)
{
CVector temp;
temp.SetX( x + param.GetX() );
temp.SetY( y + param.GetY() );
return temp;
}
CVector::CVector(const CVector &rhs)
{
cout << "Inside Copy Constructor..." << endl;
x = rhs.x + 3;
y = rhs.y + 3;
}
void DisplayCopy(CVector copy)
{
cout << "X: " << copy.GetX() << " Y: " << copy.GetY() << endl;
}
==================================================
class CVector
{
public:
CVector(int valueX = 0, int valueY = 0): x(valueX), y(valueY) {}
CVector(const CVector &rhs); // Copy Constructor
CVector operator+(CVector param);
int GetX() const { return x; }
int GetY() const { return y; }
void SetX(int valueX) { x = valueX; }
void SetY(int valueY) { y = valueY; }
protected:
int x, y;
};
void DisplayCopy(CVector copy);