Bjarne Stroustrup, being asked if...
«Can I use "new" just as in Java?»
He answered «Sort of, but don't do it blindly and there are often superior alternatives.» and showed how to do that, with the following code:
void compute(cmplx z, double d)
{
cmplx z2 = z+d; // c++ style
z2 = f(z2); // use z2
cmplx& z3 = *new cmplx(z+d); // Java style (assuming Java could overload +)
z3 = f(z3);
delete &z3;
}
I tougth that whatever the language be, the "new" works in the same manner i.e. allocates space for an object and assigns a pointer with the address of that object.
I'm wondering what's the difference (in terms of performence or whatsoever) between:
MyClass *ptr = new MyClass();
...
delete ptr;
and
MyClass& ref = *new MyClass();
...
delete &ref;
Any ideas about this?
Thank you