I am trying to convert between two classes and avoid a temporary object.
Here is my declaration for Square
:
class CSquare
{
public:
int dimension;
CSquare(int dimension);
// Conversion to Rectangle
operator CRectangle();
~CSquare(void);
};
and here is my declaration for Rectangle
:
class CRectangle
{
public:
int length;
int width;
CRectangle(int length, int width);
//Copy Constructor
CRectangle(const CRectangle& anotherRectangle);
~CRectangle(void);
};
Why does
CSquare aSquare = CSquare(10);
CRectangle convertedRect = (CRectangle) aSquare;
invoke the copy constructor?
I have a conversion function:
CSquare::operator CRectangle(){
return CRectangle(CSquare::dimension,CSquare::dimension);
}
but I'm still getting a temporary object.
How do I avoid the temporary object?