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

  • Home
  • SEARCH
  • 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 6541441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:06:21+00:00 2026-05-25T11:06:21+00:00

class A { public: virtual void f(){ printf(A.f ); } ~A(){ f(); } };

  • 0
class A
{
    public:
         virtual void f(){ printf("A.f "); }
         ~A(){ f(); }
};

class B : public A
{
    A a;

    public:
         void f(){ printf("B.f "); }
         B(){ throw -1; }
        ~B(){ f(); }
};

int main()
{
    try{ B b; }
    catch(...){ printf("Exc");}
}

So here’s how I see it. Inside the try block, nothing is being printed while constructing B b;. The block ends. I think compiler is destructing the A a; member first. So A.f() would be printed. Does that mean the destruction of class B instance is finished? After that, would compiler simply call ~A() (destructing base class)?

I thought I should’ve got A.f(), then B.f() (destructing class B instance) and after that A.f() again (destructor of base class). Compiling this made me think a little.
Exc is being printed at the end of course.
I’ve gone through several topic and haven’t found anything.

EDIT: Output from Dev-C++ (GCC 3.4.2) is

A.f A.f Exc

  • 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-25T11:06:21+00:00Added an answer on May 25, 2026 at 11:06 am

    You really have two A objects here.

    1. B inherits from A, so a base class object of A is instantiated first before B is.
    2. Another A instance is created as you have a member field of type A as part of B.

    When you create B b, you create the base class A, and also the instance A a.

    However, you then throw the exception in B‘s constructor, so then all fully-constructed objects at that point are destructed, that is.

    • ~A() is called on the instance A a.
    • ~A() is called on the base class A.

    That would explain why you get A.f A.f Exc.

    B‘s destructor would not be called because B wasn’t fully constructed as its constructor did not finish successfully.

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

Sidebar

Related Questions

Consider the following code: class A { public: virtual void f() throw ( int
class Base { public: virtual void f(int) { printf(Base f(int)\n); } virtual void f(int,
Consider the following C++ code: class A { public: virtual void f()=0; }; int
I have a question, here are two classes below: class Base{ public: virtual void
Consider the code : #include <stdio.h> class Base { public: virtual void gogo(int a){
#include <cstdio> using namespace std; class A { public: virtual void func() { printf(A::func());
I have these classes: class Base { public: virtual void foo(int x = 0)
The following code: #include <stdio.h> class Parent { public: virtual void func() {printf(Parent\n);} };
take this simple code: class A{ public: virtual void foo() = 0; void x(){
I have some classes layed out like this class A { public virtual void

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.