C++ delete syntax
I came acr开发者_如何转开发oss this rather unusual usage of 'delete'. Just wanted to know if the following line deletes both pointers or only the first?
delete ptr1, ptr2
This is undoubtedly an error. The comma here is the comma operator, not a separator. Only the first pointer, ptr1
is deleted.
The second pointer, ptr2
, is just a do-nothing expression.
The delete
operator has higher precedence than the ,
operator, so the expression is parsed as if it were written:
(delete ptr1) , (ptr2)
and not as if it were written:
delete (ptr1 , ptr2)
If ,
had higher precedence than delete
, then only the second pointer would be deleted.
James McNellis is correct that this is comma operator, but he has the operator precedence wrong. He's (apparently) thinking it works out as:
delete (ptr1, ptr2);
in which case he'd be right -- it would only delete the second item. In reality, however, delete is also an operator, and has substantially higher precedence than the comma operator (which has about as low of precedence as possible), so it really works out as:
(delete ptr1), ptr2;
So it deletes the first pointer, and the result of the expression is the value of the second pointer -- which hasn't been deleted, so it's still valid (if it was previously).
I'd echo Martin York's conclusion, and I think this backs it up. I doubt that even one whole percent of C++ programmers know C++ as well as James McNellis -- when his answer about what a construct does is wrong, it's a solid indication that almost nobody will know what it really does. I'd posit that nobody at all can be sure it's doing what was intended. In fact, I'd guess it was intended to delete both objects, which it should not do (i.e., won't unless the compiler has a bug).
[Edit: I see that while I was writing this, James has corrected his answer. My apologies to James -- but I think the basic conclusion stands, so I'm not deleting this, at least for now.]
I wouldn't recommend it, but the following will work -
delete ( delete p1, p2 );
This can be generalized as follows -
delete ( delete ( delete p1, p2 ), p3 );
delete ( delete ( delete ( delete p1, p2 ), p3 ), p4 );
Just looking at it scares me.
Don't use this even if it is legal as most people have to stop and think ( and will still get it wrong (one of the first two answers has to be wrong as they contradict each other, my first instinct is comma operator problems (but I don't know))).
Even now I would not answer the question until I had written an example and tested it and even then I would be scared of corner cases in the language that would cause problems.
Consider this sample code:
class foo
{
int _a;
public:
foo(int a) : _a(a) { }
~foo() { printf("~foo() %d\n", _a); }
};
int main(int argc, char** argv)
{
foo *p1 = new foo(1), *p2 = new foo(2);
delete p1, p2;
return 0;
}
The output is:
~foo() 1
The reason, as already answered by James, is operator precedence.
精彩评论