In the constructor of one of my classes I have this line :
m_Projects = std::vector<parent_project>(); //m_Projects type is std::vector<parent_project>
m_Current = nullptr; //m_Current type is parent_project*
m_Projects.push_back(local_project(TITLE, DEMO, FILE)); //local_project class derives from parent_project class.
m_Current = &m_Projects;
as I understand the cast would be supposed to work since m_Current ... is a local_project object.
What you are saying would be true if the
vector was containing pointers to
parent_project. But as it stores objects,
parent_project copy constructor is used to copy your
local_project(TITLE, DEMO, FILE) object when being inserted (
push_back) in the container and then the container stores a
parent_project, not a
m_Current is not a
You should change
m_Projects to be a
std::vector<parent_project*>. Then your dynamic cast will work. Make sure you delete the objects when clearing your container to avoid memory leaks then.
m_Projects.push_back(new local_project(TITLE, DEMO, FILE)); m_Current = m_Projects;
As commented by Mark Ransom, see What is object slicing?