let’s say I want to instantiate a different type of object depending on certain circumstances, so i would instantiate them inside the body of an if statement. The problem is if you want to use that object later, you need to declare it before instantiation. How does one declare a generic object. Is there something similar to the object class in Java?
I’ve done some google searching like “generic object c++” and “object class c++” and there doesn’t appear to be something like that.
Unlike Java, C++ has no “mother” object which all classes inherit from. In order to accomplish what you’re talking about, you’d need to use a base pointer of your own class hierarchy.
Also, because there’s no garbage collection either, it’s better to use smart pointers when you do this sort of thing. Otherwise, be sure you remember to
delete a. You also need to make sure thatAnimalhas a virtual destructor.