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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:32:40+00:00 2026-06-14T02:32:40+00:00

I have the following situation: I have class A that contains a stack member

  • 0

I have the following situation:

I have class A that contains a stack member object from class B:

Class A{
    B b;
}

I am using B object to redefine the behavior of the initialization and destruction of A.

Class B{
    B(){cout<<"taken care of some specific context initialization"<<endl;}
    ~B(){cout<<"cleaning the context initialization done before"<<endl;}
}

This is working so far because ~B() is being called when b is destroyed (I guess it is done after A destructor).

This is the behavior I am looking for, but I am scared about the possibility of the compiler saying: “Hey, you are not using the object, I am going to reclaim the memory before”. I guess it should to happen, but is it possible in any compiler? I mean, is it set on the standard that you have to wait for A to be destroyed?

Thanks in advance

  • 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-14T02:32:41+00:00Added an answer on June 14, 2026 at 2:32 am

    C++ follows an as-if behavior, meaning it is theoretically possible for b to be destroyed before as long as the behavior of the program isn’t affected. And if it isn’t affected, it doesn’t really matter when it is destroyed.

    In practice though, b will be destroyed right after the destructor of its owner A instance will be destroyed.

    It is guaranteed by:

    12.4 Destructors [class.dtor]

    8) After executing the body of the destructor and destroying any
    automatic objects allocated within the body, a destructor for class X
    calls the destructors for X’s direct non-variant non-static data
    members, the destructors for X’s direct base classes
    and, if X is the
    type of the most derived class (12.6.2), its destructor calls the
    destructors for X’s virtual base classes. […] (emphasis mine)

    Note that an implementation whose observable behavior acts as-if this was happening is still standard-compliant. In your particular case, since the destructor has IO operations, it’s guaranteed that this behavior actually takes place.

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

Sidebar

Related Questions

My situation is following. I have one Activity that contains three main parts. At
I have the following situation: Assembly A - Class library, contains my control Control
I have the following multi-table inheritance situation: from django.db import Models class Partner(models.Model): #
I have the following situation : abstract class X { abstract X someMethod (...)
I have following situation - Have a MongoService class, which reads host, port, database
I have following situation. In a constructor of a pseudo class I attach a
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I have the following manager<->worker situation: class Manager { private: pthread_attr_t workerSettings; pthread_t worker;
Suppose you have the following situation #include <iostream> class Animal { public: virtual void
Here's the situation: I have a custom TextBox control that contains multiple other TextBox

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.