Using * this in a C ++ class method to completely rewrite self-creation

Is the following code safe? (I already know that it compiles correctly.)

void Tile::clear()
{
    *this = Tile();
}

int main()
{
    Tile mytile;

    mytile.clear();
}
+5
source share
4 answers

It might work. It depends on how it is Tile& operator = (const Tile&);implemented. However, there is nothing wrong with assigning a *thisnew value.

+10
source

, Herb Sutter this . , , . , , , .

+1

. , - Tile, ( ), Tile, *this =, undefined.

0

It is safe if your copy instance does nothing contrary.

-1
source

All Articles