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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:42:04+00:00 2026-06-12T09:42:04+00:00

I changed my C++ base class to be protected inheritance and my dynamic_cast (s)

  • 0

I changed my C++ base class to be protected inheritance and my dynamic_cast(s) stopped working.

Why should changing the inheritance to protected change the behavior of dynamic_cast?

struct Base {
  static Base *lookupDerived(); // Actually returns a Derived * object.
};

struct Derived : protected /* Switch this to public to get it working */ Base {
 static void test() {
   Base *base = lookupDerived();

   if (dynamic_cast<Derived *>(base)) {
      std::cout << "It worked (we must be using public inheritance)." << std::endl;
   } else {
      std::cout << "It failed (we must be using protected inheritance)." << std::endl;
   }
};
  • 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-06-12T09:42:05+00:00Added an answer on June 12, 2026 at 9:42 am

    You as an outside user cannot access the protected or private members of a class. The same applies to protected or private inheritance. The authors of a class don’t want outside users to access protected/private parent classes any more than they want outside users to access protected/private members.

    One reason: Suppose the parent class has a non-virtual destructor. Deleting from an instance derived class from a base class pointer would result in undefined behavior. Making the parent class protected/private means you can’t do this (see footnote).

    Another reason: Suppose the authors of the class in question don’t want outside users to have access to the public members of the parent class. One could use public inheritance (is-a) and demote those public interfaces to protected or private, but this would violate the Liskov substitution principle. Protected or private inheritance is not an is-a relationship. Those public methods become protected or private with protected or private inheritance. There’s no problem with Liskov substitution because protected/private inheritance is not is-a.

    Footnote: There is an ugly way around this: Use C-style casts. Outside users can cast a
    derived class pointer to a base class pointer, even if the base class is not accessible. To me, that’s yet another reason to compile with -Wold-style-cast -Werror.

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

Sidebar

Related Questions

I am using a fluent interface with inheritance. I declared the base class Constructor
class base { int a; protected: template<class T> class derived; public: base() {} virtual
Consider following code: namespace base { class Base { protected: class Nested { protected:
I tried to listen file change event in BlackBerry base on FileExplorer example, but
C++11, §9/7: A standard-layout class is a class that: has no non-static data members
public class CustomEditor : Editor { protected override void Render(HtmlTextWriter writer) { Toolbar topToolbar
Every controller class in my project derive from a base controller class, aptly named
I have a base controller class: And all my other controller inherits this BaseClass
Is it possible to change the class attribute at run-time in C++ language.For example
public partial class Form1 : Form { protected override void WndProc(ref Message m) {

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.