Skip to main content

Thread: problem in returning class pointer.


code:
#include <iostream>  using namespace std;  class loc {  int longitude, latitude;  public:  loc() {}  loc(int lg, int lt) {  longitude = lg;  latitude = lt;  }  void show() {  cout << longitude << " ";  cout << latitude << "\n";  }  loc operator+(loc op2);  };  // overload + loc.  loc loc::operator+(loc op2)  {  loc temp;  temp.longitude = op2.longitude + longitude;  temp.latitude = op2.latitude + latitude;  return temp;  }  int main()  {  loc ob1(10, 20), ob2( 5, 30);  ob1.show(); // displays 10 20  ob2.show(); // displays 5 30  ob1 = ob1 + ob2;  ob1.show(); // displays 15 50  return 0;  }
in above code in operator overloading function creating local class object , returning reference. since not creating object on heap there should segmentation fault error while executing. not happening. can please explain me why happening?

quote posted c2tarun view post
code:
#include <iostream>  using namespace std;  class loc {  int longitude, latitude;  public:  loc() {}  loc(int lg, int lt) {  longitude = lg;  latitude = lt;  }  void show() {  cout << longitude << " ";  cout << latitude << "\n";  }  loc operator+(loc op2);  };  // overload + loc.  loc loc::operator+(loc op2)  {  loc temp;  temp.longitude = op2.longitude + longitude;  temp.latitude = op2.latitude + latitude;  return temp;  }  int main()  {  loc ob1(10, 20), ob2( 5, 30);  ob1.show(); // displays 10 20  ob2.show(); // displays 5 30  ob1 = ob1 + ob2;  ob1.show(); // displays 15 50  return 0;  }
in above code in operator overloading function creating local class object , returning reference. since not creating object on heap there should segmentation fault error while executing. not happening. can please explain me why happening?
why want or expect segmentation fault happen?

please use code tags enclose code, indentation remains visible.

homework?


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] problem in returning class pointer.


Ubuntu

Comments

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support