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 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

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 4219
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T15:09:07+00:00 2022-06-03T15:09:07+00:00

c++ – Inherit partially privately, partially publicly

  • 0

[ad_1]

At the moment I have an inheritance structure like this:

class B
{
    virtual void f() = 0;
};
class D1 : public B
{
    void f() override { }
};
class D2 : public B
{
    void f() override { }
};
class D3 : public D2
{
    D1* m_d1; // USUALLY available, SOMETIMES not
    void f() override
    {
        if(m_d1)
        {
            m_d1->f();
        }
        else
        {
            D2::f();
        }
    }
};

I can live with the approach at the moment, if relevant the types are detected by a parallel hierarchy and I can simply static_cast the types from B if needed – still a D3 actually shouldn’t be identifiable as a D2 (e.g. via dynamic_cast) – it’s just not clean, and could be prevented e.g. by doing

class D2 : public virtual B
{
};
class D3 : public virtual B, private D2
{
};

which would then break the static_casts, though. I’ve already thought about an aggregated member of an implementation type:

class D2 : public B
{
    Impl m_impl;
    void f() override { m_impl.f(); }
};
// D3 analogously

which, though, requires duplicating the interface yet once more (there’s actually a whole bunch of functions to consider…) and the Impl class gets visible as well (though still appears cleanest approach to me at the moment, so likely going to switch to).

Well aware that this isn’t possible I’d really have appreciated inheriting publicly only D2‘s own B part and the rest private – is there yet another technique or clever trick to get close to something alike or something cleverer as the aggregated implementation?

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8056 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1842 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1924 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

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.