Sample using "new" operator:
Object_A *A = NULL;
if (some_condition) {
A = new Object_A();
/*some code*/
} else {
A = new Object_A_Child();
/*some other code*/
}
/* some code which involves 'A' */
delete A;
Is there any way without using new
and delete
operator?
Because there are multiple such variables in my code, and it gets clumsy if I do it like that, whereas it remains very clean if I just use static variables inside if block. But the problem with that is, I have to move the common post-processing and repeat it in both if and else blocks!
PS: As per comments, the smart-pointers would atleast get rid of delete
operator.