c++ - can we create object of class based on condition statement? -


i creating bank program if acc_type current obj should created of currentclass(derived bank class) or else object should created of savingclass(derived bank class).can use this?i used showing error somelike 'obj' not declared in scope.enter image description here

if(condition) {     derivedclass1 obj; //first object } else {     derivedclass2 obj; //second object }     

the simple solution is:

baseclass *obj; if(condition) {     obj = new derivedclass1; //first object } else {     obj = new derivedclass2; //second object } 

there's other ways of doing (i'd use std::unique_ptr), easiest understand.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -