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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:05:29+00:00 2026-05-29T08:05:29+00:00

While explaining when private inheritance must be used, instead of containment, the author of

  • 0

While explaining when private inheritance must be used, instead of containment, the author of this article says the following :

“We need to construct the used object before, or destroy it after, another base subobject. If the slightly longer object lifetime matters, there’s no way to get it other than using inheritance”

If you want subobject A to be constructed before subobject B and destructed after B, wouldn’t be enough to declare A before B, in the enclosing class ? In other words, why can’t we use containment to achieve the same result in this case ?

  • 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-29T08:05:31+00:00Added an answer on May 29, 2026 at 8:05 am

    I believe that the author is talking about base subobjects, not direct subobjects. That is, you would use private inheritance if you wanted some member of the class to be constructed before the class’s other base classes would be constructed. In this case, using private inheritance will cause C++ to initialize the privately-inherited base class before other base classes, provided that you’ve inherited from them in the right order. For example, if you’re making class Derived, want a Subobject object in Derived, and inherit from Base, but you want the Subobject initialized before the Base, you could write

    class Derived: private Subobject, public Base {
    
    }
    

    And now the Subobject will be initialized before the Base.

    (That said, this is a pretty silly use case!)

    Hope this helps!

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

Sidebar

Related Questions

While I've seen rare cases where private inheritance was needed, I've never encountered a
I need some help in explaining this behavior in SQL Server 2008. I have
Here in this video at 11th minute, while explaining about strange structure (compared to
While going through university and from following the development of SO, I've heard a
While reading some documentation here , I came across this: unsigned unitFlags = NSYearCalendarUnit
Let me start by explaining this is my first Android app. So I don't
This has some lengthy background before the actual question, however, it bears some explaining
Recently while I was explaining the basic difference between pointers and references(in context of
I am working through the Programming Clojure book. While explaining alter and the STM,
I remember reading a great document a while back explaining the rationale for Git

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.