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 6212007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:26:10+00:00 2026-05-24T06:26:10+00:00

I got these virtual classes from a library that implement an algorithm in the

  • 0

I got these virtual classes from a library that implement an algorithm in the form of abstract classes

class A{
    public : 
    virtual void foo() = 0 ;
};


class B{
    public:
        void setA(A * a) { m_a = a ;}

        virtual void f() = 0;
        void g() {m_a->foo();}
    protected:
        A * m_a ;
 };

To use the library you just have to derive the classes and implement the pure virtual functions, like foo(), and provide other methods specific to the implementation as well (bar()).

class dA : public A {
    public :
         void foo() {/* ... */}
         void bar() {/* ... */}
};

class dB : public B {
     public :
          void f() ;
};

I would typically use these class by calling

dB * mydB = new dB() ;
mydB->setA(new dA() );
mydB->f() ;
mydB->g() ;

But I have a design problem when implementing dB::f(), because I need to call dA::bar() which is specific to dB. But in the class, I only keep a reference to dB trough a B*. Then I have thought of two options :

  • use a dynamic_cast each time f()is called to cast B::m_a into a dB*
  • add a m_dA member to dB which stores the same pointer as m_a, but can be used to access dB specific functions.

Of course I can’t change the the base classes.

I would like to know if there is a more elegant solution to this problem (like a design pattern I did not thought of). If not, which one should I choose ?

  • 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-24T06:26:10+00:00Added an answer on May 24, 2026 at 6:26 am

    You’ve a third solution. Add a function setA() in the dB. Of course, this function will hide B::setA() which is good for you if dB::setA() is implemeted as:

    class dB : public B 
    {
         dA *m_dA ; //add this member also!
         public :
              void f()
              {
                 m_dA->bar(); //fast forward : no cast here!
              }
              void setA(A *a) //this hides B::setA()
              {
                 m_dA= dynamic_cast<dA*>(a); //just one time dynamic cast!
                 if ( m_dA == 0 )
                 {  
                     throw std::runtime_error("invalid argument");
                 }
                 B::setA(a); //now call the hidden function in the base!
              }
    };
    

    In this way, you don’t need to dynamic_cast each time you call dB::f(), which makes the call fast!

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

Sidebar

Related Questions

I've got the following classes: public class Client { public virtual Guid ClientID {
I've got a sql query (using MS-SQL 2005) that generates data with these columns:
I've got a site that provides blog-friendly widgets via JavaScript. These work fine in
I'm eager to give these a go, but I've only got my precious home
I've got some entities which have decimal properties on them. These entities' properties are
The situation is as follows: I've got 2 models: 'Action' and 'User'. These models
Has anyone out there got a good set of instructions for building/compiling Ruby from
Hey there, I've got a block of HTML that I'm going to be using
I've got m2eclipse plugin installed and there's no problem syncing compile time dependencies that
I was happily working in C++, until compilation time arrived. I've got several classes

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.