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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:47:03+00:00 2026-05-25T01:47:03+00:00

I know that following code gives compilation error : class A{ public : virtual

  • 0

I know that following code gives compilation error :

class A{ public : virtual void name(){cout<<typeid(this).name()<<endl;}; };
class B:protected A{public : virtual void name(){cout<<typeid(this).name()<<endl;};};
void foo(B* b)
{
    A * a = dynamic_cast<A*>(b); //Error : 'A' is an inaccessible base of 'B'    
    return;
}

But then why in the C++ Stroustrup book (15.4.1) he writes

class BB_ival_slider:public Ival_slider,protected BBslider{ //...
};
void f(BB_ival_slider*p)
{  
// ok 
BBslider* pbb2 = dynamic_cast<BBslider*>(p);    // ok: pbb2 becomes 0
}

Shouldn’t the line be compilation error ?
So either my gcc is wrong in flagging it as compilation error OR the unthinkable, stroustrup typo or most plausibly I have missed something…

  • 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-25T01:47:04+00:00Added an answer on May 25, 2026 at 1:47 am

    The actual quote from 15.4.1 is:

    class BB_ival_slider : public Ival_slider, protected BBslider {
        // ...
    };
    
    void f(BB_ival_slider* p)
    {
        Ival_slider* pi1 = p; // ok
        Ival_slider* pi2 = dynamic_cast<Ival_slider*>(p); // ok
        BBslider* pbb1 = p; // error: BBslider is a protected base
        BBslider* pbb2 = dynamic_cast<BBslider*>(p); // ok: pbb2 becomes 0
    }
    

    That is the uninteresting case. However, it is reassuring to know that dynamic_cast doesn’t allow accidental violation of the protection of private and protected base classes.

    So it would seem that the text describing the code is correct, but for the wrong reasons — dynamic_cast doesn’t allow accidental violation of the protection of private and protected base classes, but only because using it would be ill-formed and will result in a compiler error, not because using it will yield a null-pointer. And, of course, the code the text is describing is definitely incorrect.

    Mistakes happen — maybe it will be fixed in the 4th edition of the book. :-]

    (Also, note that if BB_ival_slider declares f to be a friend, then the code will behave as described in the book. Perhaps this friend declaration was implied earlier in the chapter, but I don’t have time right now to read over it carefully to check one way or the other.)

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

Sidebar

Related Questions

In C++ the following code gives a compiler error: void destruct1 (int * item)
The following code gives a compiler error in C++: const double** x; const void**
Consider the following code (C# 4.0): public class Foo : LambdaExpression { } This
Consider the following code: class A { public: void foo() { auto functor =
The following code gives me this error Token Error: EOF in multi-line statement. What
I know that the following code should show and hide a tiny circular progress
The following code inside the tags gives error: Object Expected. <!-- JQuery/AJAX--> <script type=text/javascript>
I know that the following is true int i = 17; //binary 10001 int
I know that the following works but it is not that readable, is there
Okay, we know that the following two lines are equivalent - (0 == i)

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.