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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:58:04+00:00 2026-05-11T12:58:04+00:00

Will the initialization list always be processed before the constructor code? In other words,

  • 0

Will the initialization list always be processed before the constructor code?

In other words, will the following code always print <unknown>, and the constructed class will have ‘known’ as value for source_ (if the global variable something is true)?

class Foo {   std::string source_; public:   Foo() : source_('<unknown>') {     std::cout << source_ << std::endl;     if(something){       source_ = 'known';     }   } }; 
  • 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. 2026-05-11T12:58:04+00:00Added an answer on May 11, 2026 at 12:58 pm

    Yes, it will, as per C++11: 12.6.2 /10 (same section in C++14, 15.6.2 /13 in C++17):


    In a non-delegating constructor, initialization proceeds in the following order (my bold):

    • First, and only for the constructor of the most derived class (1.8), virtual base classes are 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 classes in the derived class base-specifier-list.

    • 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).

    • Finally, the compound-statement of the constructor body is executed.


    The main reason for using init-lists is to help the compiler with optimisation. Init-lists for non-basic types (i.e., class objects rather than int, float, etc.) can generally be constructed in-place.

    If you create the object then assign to it in the constructor, this generally results in the creation and destruction of temporary objects, which is inefficient.

    Init-lists can avoid this (if the compiler is up to it, of course but most of them should be).

    The following complete program will output 7 but this is for a specific compiler (CygWin g++) so it doesn’t guarantee that behaviour any more than the sample in the original question.

    However, as per the citation in the first paragraph above, the standard does actually guarantee it.

    #include <iostream> class Foo {     int x;     public:         Foo(): x(7) {             std::cout << x << std::endl;         } }; int main (void) {     Foo foo;     return 0; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Do you need both the "inner" and "outer" items to… May 12, 2026 at 12:04 am
  • Editorial Team
    Editorial Team added an answer Have you looked at their external pagination and sorting? We… May 12, 2026 at 12:04 am
  • Editorial Team
    Editorial Team added an answer Instead of invoking the compiler directly, consider using MSBuild on… May 12, 2026 at 12:04 am

Related Questions

In C++, it's not possible to initialize array members in the initialization list, thus
I am struggling with the best way to handle a WPF application that has
I know that if you leave a member out of an initialization list in
I'm trying to create a Fluent Interface to the Winforms Datagrid. This should allow

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.