void fun() { *a = new a; //here class } //a should deleted in fun()'s scope int main() { fun(); return 0; }
the object created exists on free store , cannot used main() function. why should objects created on free store. yes can pass object reference main function can pass copy of object(even when not created using new operator). what's exact use of new , delete operator?
in example, there none, , isn't practice use dynamic allocation. dynamic allocation used when objects have identity (or cannot copied other reason), , lifetime of object not correspond pre-determined lifetime, static or auto. dynamic allocation may used in few cases copying expensive, , profiler shows copying bottleneck; in such cases, using dynamic allocation , copying pointer may remove bottleneck. (but should never done until profiling shows necessary.)