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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:01:50+00:00 2026-05-16T22:01:50+00:00

How to implement casting to a private base class in C++? I don’t want

  • 0

How to implement casting to a private base class in C++? I don’t want to use hacks such as adding a friend etc. Defining public casting operator does not work.

EDIT :

For example I have:

class A {
//base class
}

class AX : private A {
//a child
}

class AY : private A {
//another specialized child
}

class B {
//base class
void do (A a) {//do
    }
}

class BX : private B {
//a child
void do (AX a) {
     B::do( static_cast <A> (a) );
    }
}

class BY : private B {
//another specialized child
void do (AY a) {
    B::do( static_cast <A> (a) );
    }
}

EDIT2

Why do I do this?

Suppose I have to define some property which is quite heavyweight and can be of several similar types (like VelocityX VelocityY etc). Then I want to be able to have classes which can have any set of these properties. If I want to process these properties, it is obvious, that I’d rather cast them to their base type than to add an implementation for each variation. I do not use public inheritance because it’s better to explicitly cast where needed than to have the private interface implicitly visible. Not a real problem but I’d like to have a solution 🙂

  • 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-16T22:01:51+00:00Added an answer on May 16, 2026 at 10:01 pm

    If defining a public casting operator does not work, you can try with a regular function:

    class D: private B {
        public:
            B& asB() { return static_cast<B&>(*this); }
    };
    ...
    D d;
    d.asB().methodInB();
    ...
    

    Anyway, what is the point? If D derives privately from B, then you are not supposed to use a D as a B from the outside.

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

Sidebar

Related Questions

System.Threading.WaitHandle (and hence ManualResetEvent etc) implement IDisposable explicitly. This seems to discourage it's use
I'm writing a custom class that wraps a dict. As such, I want to
Is it possible to implement a class template in such a way that one
If I have a base class and two derived classes, and I want to
I have a generic class that I'm trying to implement implicit type casting for.
I want to call a specific operator of specific base class of some class.
Anybody know how to implement a casting rule in C++ for one class? Let's
I implement notifications in my IOS app, and I use customized sounds. Everything works
I want to implement a simple script to do some boring housekeeping on my
I have a main class and several inherited classes that implement a method with

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.