Assume I have the following code snippet for GPoint
, which has a copy constructor, assignment operator, and destructor. The same for GCircle
and this has a function named GetCentre()
which returns a copy of the Gpoint
object(Centre
).
In main
or ButtonClick()
, as below is it safe/valid to call GPoint &Centre = circle.GetCentre()
? By doing this (if valid) we would save the time of calling assignment operator!.
class GPoint
{
public:
GPoint();
virtual ~GPoint();
GPoint(double p_dX, double p_dY);
GPoint (const GPoint &Source);
GPoint& operator = (const GPoint &point);
public:
double c_y;
double c_x;
};
class GCircle//:public GShape
{
public:
GCircle();
GCircle(GPoint p_point, double p_dRadius);
~GCircle(){}
operator GPoint&();
operator double&();
double& GetRadius() const ;
GPoint GetCentre() const {return c_Centre;} //Return copy Not a reference
public:
double c_dRadius;
GPoint c_Centre;
};
Dlg::ButtonClick()
{
GPoint Point1(10,2);
GCircle circle(Point1, 100);//100 is the radius.
**GPoint &Centre = circle.GetCentre();** //is this reference safe/valid or invalid
}