0

For example, this:

Class Point{
  double x, y;

public:
     Point();
     bool testequal(const Point& p1, Point& p2 = Point()) const;
}

doesn't work. It gives me an error:

error: could not convert 'Point()' from Point to Point&

This works if I use it as,

bool testequal(const Point& p1, const Point& p2 = Point()) const;

or

bool testequal(const Point& p1, Point p2 = Point()) const;

But instead I want to use object p2 as a reference value whose data can be changed inside the implementation.

Edit:

Here is the complete program. Yes, it's trivial - I'd prefer it if you wouldn't assess the need for this code, but instead comment on whether it's possible to implement.

If not, could you please state why, and tell me what the right way to do it is. Is overloading the only option?

#ifndef __POINT_H__
#define __POINT_H__

Class Point{
  double x, y;

public:
     Point();
     Point(double xx, double yy);
     bool testequal(const Point& p1, Point& p2 = Point()) const;
// this declaration fails. alternative is to use const Point& p2 or Point p2. 
// but it is vital that the default parameter should store some value in 
// it which can be accessed at the function call location without returning it.
}


#include "Point.h"

Point::Point(): x(0), y(0) {}

Point::Point(double xx, double yy): x(xx), y(yy) {}

bool Point::testequal(const Point& p1, Point& p2){
    if (this->x == p1.x && this->y == p1.y){
        p2.x = this->x;
        p2.y = this->y;
        return true;
    }
    else
        return false;
}
ali_m
  • 71,714
  • 23
  • 223
  • 298
kkprime
  • 138
  • 9
  • 1
    The first way cannot work, why would you want to store a reference to a temporary? You'll be stuck with a dangling reference. It must either be by value or a const reference, which will extend the temporary's lifetime. – Cory Kramer Jun 29 '15 at 17:11
  • 3
    temporary cannot be bound to non const reference. – Jarod42 Jun 29 '15 at 17:11
  • This is not valid C++ syntax, putting aside the question you're asking about. Show us the _actual_ code that you're _actually_ compiling. – Lightness Races in Orbit Jun 29 '15 at 17:14
  • Why should a function named `testequal` modify one of its operands? That makes no sense. – Lightness Races in Orbit Jun 29 '15 at 17:15
  • @CoryKramer: I think it's obvious the OP expected the temporary to live for the duration of the function call, regardless. – Lightness Races in Orbit Jun 29 '15 at 17:16
  • @LightnessRacesinOrbit The example was a trivial one to get the sol to the question. The actual code tests if two lines intersect or not. And the default parameter is to store the point of intersection. – kkprime Jun 29 '15 at 18:17
  • @CoryKramer Am trying to find the right syntax where an object can be used as a default parameter without const reference. – kkprime Jun 29 '15 at 18:18
  • Huh yeah true since it's a member function my previous comment was pretty stupid. But then again so is your variable naming and lack of comments :) – Lightness Races in Orbit Jun 29 '15 at 19:17
  • Why are people re-opening this, while the edit is essentially asking a follow-up question, invalidating the existing answers... – Sumurai8 Jun 29 '15 at 19:29

1 Answers1

4

You may add an overload:

bool testequal(const Point& p1, Point& p2) const;
bool testequal(const Point& p1) const
{
    Point dummy;
    return testequal(p1, dummy);
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302