As high_flyer mentioned, you not very clear in how inheritance in C++ works, (or at-least how to use it, in your case).
This is not correct, you should be using new DerivedClass, or a factory method to create object and get it's base class pointer. If you do it this way, you are basically creating a BaseClass Object and store it in BaseClass pointer, where as you want to create a DerivedClass object, and store it in BaseClass pointer.Qt Code:
Baseclass *entity = new Baseclass;To copy to clipboard, switch view to plain text mode
This is not possible, you need to know the type at compile time to cast it to, where as typeid operator returns a type_info class object (which happens at runtime), and again which is of type type_info, not your BaseClass (as you expect)*entity = *dynamic_cast<(typeid(m_listEntities.at(m_listEnti ties_selected[i]) ) )> (m_listEntities.at(m_listEntities_selected.at(i))) ;//errors
you can use the typeid operator to get type information like, this
Conclusion:typeid(int).name()
typeid(derived).name() //parameter can be object, class, or object pointer..
if(typeid(object1) == typeid(object2)) //this is run time object creation, and uses the type_info class == operator to compare the objects
...
To create a DerivedClass object, you cannot avoid if..else (or switch case), to know the type(or other criteria) to create object. The best practice is either have factory class, or a factory method, which is the solution provided by the factory design pattern for your problem.
I suggest you reading about factory design pattern, which is cleaner, maintainable (adding new derived classes in future) solution to your problem.
Bookmarks