See this code here:
class test
{
int n;
int *j;
public:
test(int m)
{
n = 12;
j = new int;
cin >> *j;
}
void show()
{
cout << n << ' ' << *j << endl;
}
~test()
{
delete j;
}
};
int main()
{
test var = 123;
var.show();
return 0;
}
In this program compiler should complain about double deleting of j
. First delete is done when temporary object temporary(123)
is destroyed. The second delete is done when var
object is destroyed. But this is working fine?
Does it mean temporary object does not call destructor?
The contentious line is this:
test var = 123;
The relevant standard text (that the pundits in the comments are referencing), I believe, is (8.5, "Declarators"):
The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. The temporary is an rvalue. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized;
Indeed, in 12.6, we get an example of this:
complex f = 3; // construct complex(3) using
// complex(double)
// copy it into f
Thus, in your use of =
, your implementation is probably directly constructing object and eliminating the intermediate temporary entirely (and, as the comments have noted, most do).
This class doesn't copy properly, so creating a copy of it (and the freeing the copy and the original) would result in a double delete (and crashes, undefined behavior, etc.). Because no copies are created, this scenario does not happen above.