Should downcasting be avoided while using a class hierarchy in C++?

Posted by neuviemeporte on Stack Overflow See other posts from Stack Overflow or by neuviemeporte
Published on 2011-01-11T12:34:47Z Indexed on 2011/01/11 12:53 UTC
Read the original article Hit count: 195

Filed under:
|
|

Let's say I'm writing an application which works with projects, and exposes different functionality depending on the type of the project. I have a hierarchy of classes for the different types of projects:

class AbstractProject
{
};

class ProjectA : public AbstractProject
{
};

class ProjectB : public AbstractProject
{
};

class ProjectC : public AbstractProject
{
};

Now, I was planning to have an AbstractProject *_currentProject pointer as a member in the application's main class, pop up a dialog box on startup and based on the selection, do:

_currentProject = new ProjectB(); // e.g.

Later, I'll have to downcast the pointer to the specific type to utilize the functionality specific to different Project-s. Somehow this makes me feel uneasy. Is there a Better Way of doing this?

© Stack Overflow or respective owner

Related posts about c++

Related posts about inheritance