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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:48:39+00:00 2026-05-25T19:48:39+00:00

A little question about creating objects. Say I have these two classes: struct A{

  • 0

A little question about creating objects. Say I have these two classes:

struct A{
    A(){cout << "A() C-tor" << endl;}
    ~A(){cout << "~A() D-tor" << endl;}
};

struct B : public A{
    B(){cout << "B() C-tor" << endl;}
    ~B(){cout << "~B() D-tor" << endl;}

    A a;
};

and in main I create an instance of B:

int main(){
    B b;
}

Note that B derives from A and also has a field of type A.

I am trying to figure out the rules. I know that when constructing an object first calls its parent constructor, and vice versa when destructing.

What about fields (A a; in this case)? When B is created, when will it call A‘s constructor? I haven’t defined an initialization list, is there some kind of a default list? And if there’s no default list? And the same question about destructing.

  • 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-25T19:48:40+00:00Added an answer on May 25, 2026 at 7:48 pm
    • Construction always starts with the base class. If there are multiple base classes then, construction starts with the left most base. (side note: If there is a virtual inheritance then it’s given higher preference).
    • Then the member fields are constructed. They are initialized in the
      order they are declared
    • Finally, the class itself is constructed
    • The order of the destructor is exactly the reverse

    Irrespective of the initializer list, the call order will be like this:

    1. Base class A‘s constructor
    2. class B‘s field named a (of type class A) will be constructed
    3. Derived class B‘s constructor
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First question on here so please be nice :) I know very little about
This is my little big question about containers, in particular, arrays. I am writing
i have a question about the function call in the following example: int main()
I'm kind of new to writing sql and I have a question about joins.
I have little program creating a maze. It uses lots of collections (the default
I have a question about writing your own init methods in objective-c. I've read
I have several little questions about best practices with JPA (via hibernate). My first
My question seems easy but is little more theoretical than it looks. There are
I know this question might sound a little cheesy but this is the first
This might sound like a little bit of a crazy question, but how can

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.