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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:47:30+00:00 2026-06-10T18:47:30+00:00

Possible Duplicate: Member fields, order of construction If i have a class with two

  • 0

Possible Duplicate:
Member fields, order of construction

If i have a class with two members like this:

class A
{
    int a;
    int b;
    A() {}
};

Is the order in which a and b are constructed undefined?

If I use cl, then no matter in which order I call the constructors, the members are always constructed in the order in which they are declared in the class. In this case it would always be a then b, even if I define the constructor for A like:

A() : b(), a() {}

But I am assuming that that is just the behaviour of the specific compiler.

  • 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-10T18:47:31+00:00Added an answer on June 10, 2026 at 6:47 pm

    No. Members are constructed in the order in which they are declared.

    You are advised to arrange your initializer list in the same order, but you are not required to do so. It’s just very confusing if you don’t and may lead to hard-to-detect errors.

    Example:

    struct Foo {
        int a; int b;
        Foo() : b(4), a(b) { }  // does not do what you think!
    };
    

    This construction is actually undefined behaviour, because you’re reading an uninitialized variable in the initializer a(b).


    Standard reference (C++11, 12.6.2/10):

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

    — Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).

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

Sidebar

Related Questions

Possible Duplicate Why can't I have a non-integral static const member in a class?
Possible Duplicate: Why is a class allowed to have a static member of itself,
Possible Duplicate: Virtual Functions and Performance C++ Is this correct, that class member function
Possible Duplicate: Start thread with member function I have a small class: class Test
Possible Duplicate: C# member variable initialization; best practice? Is there any benefit to this:
Possible Duplicate: CakePHP: Call to a member function find() on a non-object I have
Possible Duplicate: Start thread with member function I have recently been playing around with
Possible Duplicate: GCC problem : using a member of a base class that depends
Possible Duplicate: c++ publicly inherited class member cannot be used as default argument Nonstatic
Possible Duplicate: Initializing private static members Why I can't initialize non-const static member or

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.