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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:02:34+00:00 2026-05-27T04:02:34+00:00

I deliberately create the following code to let me easily present the question. Question>

  • 0

I deliberately create the following code to let me easily present the question.

Question> What is the initialization order of all variables?

#include <iostream>

int iGlobal = 10;

class A {
public:
    A(int _a) : m_a_a(_a) {}

private:
    int m_a_a;
};

class B : public A
{
public:
    B() : m_b_b(40), A(20), m_b_a(30) {}

private:
    static int m_b_static_a;
    int m_b_a;
    int m_b_b;
    int m_b_c; // this variable is NOT initialized in the B::B() initialization list
    static int m_b_static_b;
};

int B::m_b_static_a = 11;
int B::m_b_static_b = 12;

int main(void)
{
   B b;
   return 0;
}

First, I list all variables as follows:

iGlobal, m_a_a, m_b_static_a, m_b_a, m_b_b, m_b_c, m_b_static_b

Here is what I think I am right.

m_a_a < m_b_a < m_b_b < m_b_c (i.e. X < Y iff X is initialized early than Y)

m_b_static_a < m_b_static_b

Rule1> C++ guarantees that variables in compilation unit (.cpp file) are initialised in order of declaration.

Rule2> The order listed in the initialized list doesn’t control the order of initialization.

I have problems to order among the global variable, static variable and non-static variable.

  • 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-27T04:02:34+00:00Added an answer on May 27, 2026 at 4:02 am

    Within a compilation unit, globals are initialised in the same order they’re declared. However, the order is unspecified across different compilation units (see What’s the “static initialization order fiasco”?).

    Class initialisation order:

    1. Members of base classes (when inheritance is present)
    2. Members of the current class
    3. Constructor

    Members initialisation respects the declaration order, regardless the order used in the initilisation list.

    Having class C : public A, public B, initialiases A, then B, then C. And their destruction occurs in the inverse order.

    NOTE: Virtual base classes do not respect the order mentioned above.

    Quoting §12.6.2/5 from the C++03 Standard:

    Initialization shall proceed in the following order:

    — First, and only for the constructor of the most derived class as
    described below, virtual base classes shall be initialized in the
    order they appear on a depth-first left-to-right traversal of the
    directed acyclic graph of base classes, where “left-to-right” is the
    order of appearance of the base class names in the derived class
    base-specifier-list.

    — Then, direct base classes shall be initialized in declaration order
    as they appear in the base-specifier-list (regardless of the order of
    the mem-initializers).

    — Then, nonstatic data members shall be initialized in the order they
    were declared in the class definition (again regardless of the order
    of the mem-initializers).

    — Finally, the body of the constructor is executed.

    Concluding, the order will be:

    iGlobal (global)
    B::m_b_static_a (static)
    B::m_b_static_b (static)
    A::m_a_a (base class member)
    B::m_b_a (class member)
    B::m_b_b (class member)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Relevant code is this: typedef unsigned long int chunk_head; typedef struct malloc_chunk { //
Okay, suppose I have a bunch of variables, one of them declared volatile: int
We all know when to use include guard, but when shall we not use
I have the following input element (I deliberately omitted attributes which weren't necessary for
I have a program which deliberately performs a divide by zero (and stores the
What is the best way to deliberately delay a response from the JBoss server?
I ran gem sources -c so that I have to deliberately specify where I
I'm trying to do a fuzzy match on the Phrase Grand Prarie (deliberately misspelled)
I have this code I been working on but I'm having a hard time
I'm trying to create some 'friend assemblies' using the [InternalsVisibleTo()] attribute, but I can't

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.