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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:18:03+00:00 2026-06-01T00:18:03+00:00

I am trying a code about base class and member construction and destruction and

  • 0

I am trying a code about base class and member construction and destruction and I am confused about some order of constuctor and destructor,
the output of this code is:

Base1 constructor
Member1 constructor
Member2 constructor
Derived1 constructor
Member3 constructor
Member4 constructor
Derived2 constructor
Derived2 destructor
Member4 destructor
Member3 destructor
Derived1 destructor
Member2 destructor
Member1 destructor
Base1 destructor

See the first four line,
but I fell the order should be

Base1 constructor
Derived1 constructor
Member1 constructor
Member2 constructor

Anyone can give me some explain?

#include "stdafx.h"
#include <fstream>
using namespace std;
ofstream out("order.out");

#define CLASS(ID) class ID { \
public: \
  ID(int) { out << #ID " constructor\n"; } \
  ~ID() { out << #ID " destructor\n"; } \
};

CLASS(Base1);
CLASS(Member1);
CLASS(Member2);
CLASS(Member3);
CLASS(Member4);

class Derived1 : public Base1 {
  Member1 m1;
  Member2 m2;
public:
  Derived1(int) : m2(1), m1(2), Base1(3) {
    out << "Derived1 constructor\n";
  }
  ~Derived1() {
    out << "Derived1 destructor\n";
  }
};

class Derived2 : public Derived1 {
  Member3 m3;
  Member4 m4;
public:
  Derived2() : m3(1), Derived1(2), m4(3) {
    out << "Derived2 constructor\n";
  }
  ~Derived2() {
    out << "Derived2 destructor\n";
  }
};

int main() {
  Derived2 d2;
} ///:~
  • 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-01T00:18:05+00:00Added an answer on June 1, 2026 at 12:18 am

    Constructors are called upwards in the hierarchy:

    - base class member objects
    - base class constructor body
    - derived class member objects
    - derived class constructor body
    

    The output is correct.

    Let’s simplify your code:

    struct BaseMember
    {
       BaseMember() { cout << "base member" <<endl; }
    };
    struct Base
    {
       BaseMember b;
       Base() { cout << "base" << endl; }
    };
    struct DerivedMember
    {
       DerivedMember() { cout << "derived member" << endl; }
    };
    struct Derived : public Base
    {
       DerivedMember d;
       Derived() { cout << "derived" << endl; }
    };
    
    Derived d;
    

    When d is created, it will first create the Base part. Before it enters the constructor body, all member objects are initialized. So BaseMember is the first object initialized.

    Next, the constructor of Base is entered.

    Before, the constructor of Derived enters, member objects of Derived are initialized, so DerivedMember is created, next Derived constructor is called.

    This happens because when you enter the constructor body of a derived class, base classes and member objects must be completely initialized.

    EDIT As Matthieu pointed out, the order in which the member objects are initialized is specified by the order in which they appear in the class definition, not the order in which they appear in the initializer list.

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

Sidebar

Related Questions

I'm trying to learn linked-lists in Java and had some questions about the code
I'm trying to learn about this feature of javascript I keep seeing in code,
First off, I will be talking about some legacy code and we are trying
Good evening, I have been trying to wrap my head about this, below, code
I'm trying to use member variables of a templated base class in a derived
I would expect the following code snippet to complain about trying to assign something
I am trying to setup a repository of reusable code. I was thinking about
I am having an issue converting type. I was trying code like this (minimal,
This is more of a conceptual question than anything. I have a base class
I'm trying to create a generic method to use in my base class for

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.