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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:08:18+00:00 2026-05-20T18:08:18+00:00

I came across this issue accidentally when I was going over inheritance and up/down

  • 0

I came across this issue accidentally when I was going over inheritance and up/down casting. Why is this not allowed (code is commented to show sections that are not allowed)? Now I can guess as to why it is not allowed but a factual answer would be great.

As for the code that is allowed, I know it is because (Base*) is a C-style cast which is essentially a reinterpret_cast in C++ which in turn means that in this case it will result in undefined behavior. Please correct me if I am wrong.

class Base
{
};

class Derived : public Base
{
};

class DerivedProt : protected Base
{
};

class DerivedPriv : private Base
{
};

int main()
{
  Base* a = new Derived();
  Base* b = new DerivedProt();  // Not allowed
  Base* c = new DerivedPriv();  // Not allowed

  Base* d = (Base*) new DerivedProt(); // Allowed but undefined behavior...?
  Base* e = (Base*) new DerivedPriv(); // Allowed but undefined behavior...?
}
  • 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-20T18:08:19+00:00Added an answer on May 20, 2026 at 6:08 pm

    Sounds like you are correct.

    One thing to remember is that traditional OO principles such as the LSP only describe public inheritance. Non-public inheritance falls in-between inheritance and composition, the base subobject is non-public like composition, but you can also take advantage of features which rely on inheritance, such as virtual functions.

    Just like a composed subobject, however, only the class (or its descendants, in case of protected inheritance), can get the address of the subobject.

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

Sidebar

Related Questions

While writing some code i came across this issue: #include <iostream> class random {
While debugging a crash, I came across this issue in some code: int func()
I came across this issue when refactoring code recently: The method getList() below has
While researching the issue of JSON vs XML , I came across this question
I came across this snippet of code on MSDN: entityBuilder.Metadata = @res://*/AdventureWorksModel.csdl| res://*/AdventureWorksModel.ssdl| res://*/AdventureWorksModel.msl;
I came across this code today AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null); Is there anything wrong with
I came across this issue recently and thought it was really helpful. My question
I'm using iText to generate PDF reports when I came across this issue, and
I am not sure what causes this error but since I came across this
Came across this error today. Wondering if anyone can tell me what it means:

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.