私は(私のコンパイラがそう言う!)、これは間違っている実現:メモリリークを起こさずにローカル/一時オブジェクトへの参照を返すか?
Rectangle& Rectangle::Overlap(const Rectangle& rectangle) {
Point topLeft(__max(this->GetVerticies()[0]->GetX(), rectangle.GetVerticies()[0]->GetX()) - __min(this->GetVerticies()[0]->GetX() + this->GetWidth(), rectangle.GetVerticies()[0]->GetX() + rectangle.GetWidth()),
(__max(this->GetVerticies()[0]->GetY(), rectangle.GetVerticies()[0]->GetY()) - __min(this->GetVerticies()[0]->GetY() + this->GetHeight(), rectangle.GetVerticies()[0]->GetY() + rectangle.GetHeight())));
Point bottomRight(__min(this->GetVerticies()[0]->GetX() + this->GetWidth(), rectangle.GetVerticies()[0]->GetX() + rectangle.GetWidth()), topLeft.GetY() + __max(this->GetVerticies()[0]->GetY() + this->GetHeight(), rectangle.GetVerticies()[0]->GetY() + rectangle.GetHeight()));
return Rectangle(topLeft, bottomRight);
}
メモリリークを発生させずに計算された矩形を返すための正しい方法でしょうか? Rectangle * result = new Rectangle(topLeft、bottomRight)として矩形を定義すると、逆参照されたポインタが返されますが、間違っているようです。助言がありますか?
これはメモリリークではありません。逆に、実際にアクセスする前にメモリ(Rectangle()コンストラクタを呼び出すと作成される一時変数)を解放しています。回避策については、hkasierの答えを参照してください。 –