Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6193919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:12:17+00:00 2026-05-24T03:12:17+00:00

I have a design based question where I want to avoid dynamic_cast but still

  • 0

I have a design based question where I want to avoid dynamic_cast but still want to know how to get to loose coupling.

 class Prod { // Prod has lot of subclasses 
 public:    
      void method()
      {//Some Implementation
      } 
 };

 class Prod_X : Prod 
 { 
     int special_variable; 
     public:    
         void method()
            {//Override Base
            }    
         void setSpecialVariable() 
            {//Set Special Variable
            } 
 };

 class Factory 
 { 
 public: 
      Prod* create(string &s) 
      { if (s == "X") return new Prod_X; //And so on
      };
 };

  class O 
  { 
  public: 
       Factory F; // Assume we are talking about a simple factory pattern 
       Prod* create(string &s) 
       { p = F.create(s); return p;}
       Prod* p; 
  };


 // Issue is here on coupling and how to avoid dynamic_cast
 // Inherited Prod member for O_derived_X is always Prod_X (Factory takes care of that) 
 class O_derived_X { 
 int special_variable; 
 public: 
      void setSpecialVariable() 
      { // Need to set Prod_X Variable 
        Prod_X *px = dynamic_cast<Prod_X*>(p); 
        px->setSpecialVariable(); 
      } 
 };

Two things

  1. I introduced the special_variable in Prod_X because it was an attribute of Prod_X and not Prod in general. Is this right?
  2. class O basically uses the interface of class Prod to do mostly everything. But, for this special variable, O_derived_X is interested in setting it correctly.

Could you suggest where I am going wrong? Or how can I refactor the code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:12:18+00:00Added an answer on May 24, 2026 at 3:12 am

    In an ideal design, publicly inherited classes have an “is a” relation and not an “is an extension of” relation in the sense that they may be extended but you don’t notice it from the interface point of view. This is best achieved if you actually order objects to do something and you don’t get/set them. In that case factories can just create what is needed and clients don’t need to know the actual class (polymorphism).

    This is not always possible/easy, so casting could be the solution, but you could argue that a factory may not be needed in that case (O_derived_X could create a Prod_X object itself).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

tl;dr-edition: I have a compilation I know that will fail, but want the subset
I have started design of a ColdFusion application that is entirely web based. Not
I have existing java code and need to create Design Document based on that.
I have this design which I cannot seem to get right, I would like
the title might be too generic, but I have a very specific question about
i have a question that is more design and architecture related. I am coming
This is a very beginner question. I have tried to search for advice but
I have a C# (2008/.NET 3.5) class library assembly that supports WPF (based on
I am working in asp.net MVC2 application and I have question related to design
I have a weird design question. I have a model called Article, which has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.