What is the problem of my code ?

In this code , Although I dont use from reference (&) in overloading "=" operator but my code doing correct . what is problem ?
amiref wrote:
In this code

What code?
I am sorry Athar.
In This Code :
#include <iostream>
#include <conio.h>
using namespace std;

class loc
{
private:
int longitude;
int latitude;
public:
loc() {} //needed to construct temporaries
loc(int lg, int lt)
{
longitude = lg;
latitude = lt;
}

void show()
{
cout << longitude << "\t";
cout << latitude << endl;
}

loc operator+(loc op2);
loc operator-(loc op2);
loc operator=(loc op2);
loc operator++();
};
//******************
//overload + for loc
loc loc::operator+(loc op2)
{
loc temp;
temp.longitude = op2.longitude + longitude;
temp.latitude = op2.latitude + latitude;
return temp;
}
//********** overload - for loc ***********
loc loc::operator-(loc op2)
{
loc temp;
//notice order of operand
temp.longitude = longitude - op2.longitude;
temp.latitude = latitude - op2.latitude ;
return temp;
}
// overload assign for loc
loc loc::operator=(loc op2)
{
longitude = op2.longitude;
latitude = op2.latitude ;
return *this; // return object that generated call
}
//******* overload prefix ++ for loc
loc loc::operator++()
{
longitude ++;
latitude ++ ;
return *this; //return object that generated call
}
//**********************
int main()
{

loc ob1(10, 20), ob2(5, 30), ob3(90, 90);
cout << "ob1=";
ob1.show(); //display 10 , 20
cout << "ob2=";
ob2.show(); //display 5, 30
++ob1;
cout << "ob1=";
ob1.show(); //display 11 , 21
ob2 = ++ob1;
cout << "ob1=";
ob1.show(); //display 12 , 22
cout << "ob2=";
ob2.show(); //display 12, 22
ob1 = ob2 = ob3; //multiple assignment
cout << "ob1=";
ob1.show(); //display 90, 90
cout << "ob2=";
ob2.show(); //display 90, 90
cout << "ob3=";
ob3.show(); //display 90, 90
getch();
return 0;
}
Well, since you're passing op2 by value in operator=, the object is copied by invoking the copy constructor.
That will be slower than passing by reference for large and complex objects, but will usually have the same results if the copy contructor is properly implemented.
Topic archived. No new replies allowed.