I am expanding on a interview question I had. I created a Rectangle class and instantiated a few of them. I encountered a problem when the deconstructors are called. r1 was fine. r2 caused a crash. So I manually called the deconstructors and noticed it was crashing on r2. I thought maybe it was linked to r1 when it was deleted but that was not it. The problem started before.
I am changing the point value of r2 and printing it out. The change was fine till I get to the cout. r2 Before http://codyccrawford.com/images/Temp/r2Before.png r2 After http://codyccrawford.com/images/Temp/r2AfterSet.png The value of point changes when entering my overloaded operator <<. The incoming variable rect get garbage for the point, which changes r2. rect garbage http://codyccrawford.com/images/Temp/rectWrong.png r2 Wrong http://codyccrawford.com/images/Temp/r2AfterCout.png
Setting r2's point and printing out:
r2.SetPoint({ 6.0f, 1.0f }); // Move r2
std::cout << "After moving r2: " << &r2 << std::endl;
My overloaded operator:
std::ostream& operator<<(std::ostream & out, Rectangle* rect)
{
return out << "x: " << rect->GetPoint()->x
<< " y: " << rect->GetPoint()->y
<< " width: " << rect->GetDimensions()->width
<< " height: " << rect->GetDimensions()->height;
}
Why is r2 changing from the print statement?
To see all my code see my repository. (Please feel free to give me advice on my code and standards)
Aucun commentaire:
Enregistrer un commentaire