reference - Return object from a method c++ -


i created class in program

node.h

class node { private:     std::string id;     double latitude;     double longitude; public:     node();     node(const node& orig);     node(std::string id, double lat, double lon);     virtual ~node();      void setlongitude(double longitude);     double const & getlongitude() const;      void setlatitude(double latitude);     double const & getlatitude() const;      void setid(std::string id);     std::string const & getid() const; }; 

node.cpp

    node::node() { }  node::node(const node& orig) {      this->id = orig.id;     this->latitude = orig.latitude;     this->longitude = orig.longitude; }  node::~node() { }  node::node(std::string id, double lat, double lon){     this->id = id;     this->latitude = lat;     this->longitude = lon; }  void node::setlongitude(double longitude) {     this->longitude = longitude; }  double const & node::getlongitude() const {     return longitude; }  void node::setlatitude(double latitude) {     this->latitude = latitude; }  double const & node::getlatitude() const {     return latitude; }  void node::setid(std::string id) {     this->id = id; }  std::string const & node::getid() const {     return id; } 

i created method returning node

node& geomertyhelper::getperpendicularpoint(double pointlat, double pointlon, node front, node back) const {     node node;     node.setlatitude(x4);     node.setlongitude(y4);      return node; } 

then i'm getting values method.

node projectionpoint = geomertyhelper::instance().getperpendicularpoint(lat, lon,                 node("",nearestnode.getlatitude(), nearestnode.getlongitude()),                 node("",frontnodelat, frontnodelon));  double frontnodelat1 = projectionpoint.getlatitude(); double frontnodelon1 = projectionpoint.getlongitude();  std::cout << std::fixed; std::cout << std::setprecision(7) << "dis= lat------->>>>>" << frontnodelat1; std::cout << std::setprecision(7) << "dis= lon------->>>>>" << frontnodelon1 << std::endl; 

but i'm getting 0s values.

your returning reference temporary object in geomertyhelper::getperpendicularpoint. should let method return node instead of node& since current method results in undefined behaviour.

edit: allocating , returning node* work, too. shouldn't necessary since node object wouldn't copied when returning due return value optimization anyways.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -