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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:53:23+00:00 2026-06-13T20:53:23+00:00

Is there any benefit to putting a class member variable in an initializer list

  • 0

Is there any benefit to putting a class member variable in an initializer list that doesn’t need to be in an initializer list? Example:

class Foo
{
public:
  Foo() {}
};

class Bar
{
public:
  Bar() : _foo() {}

private:
  Foo _foo;
};

Does the compiler do anything special, in this case?

  • 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-13T20:53:25+00:00Added an answer on June 13, 2026 at 8:53 pm

    In this case it makes no difference.

    But it can be useful to do so.
    If you have a large number of members then having some but not all members in the list can cause some confusion. Also it reinforces in your mind the order of initialization (the order is defined by the order of declaration in the class, but it can be useful to visualize this order in larger classes where not all the member variables are declared beside each other).

    Note: If you put them in the wrong order in the internalizer list this is usually just a warning on most compilers (unless you compile with warnings as errors (which you should)).

    The real danger is with classes that have a POD members and compiler generated constructor.

    class NewFoo
    {
          int x;
          int y;
    };
    // Version 1:
    class Bar1
    {
         NewFoo  f;
    };
    // Version 2:
    class Bar2
    {
         NewFoo  f;
         public:
             Bar2()  // f not in list.
             {}
    };
    // Version 3:
    class Bar3
    {
         NewFoo  f;
         public:
             Bar3()
                : f()
             {}
    };
    
    int main()
    {
         Bar1     b1a;           // x and y not  initialized.
         Bar1     b1b = Bar1();  // x and y zero initialized.
         Bar2     b2a;           // x and y not  initialized.
         Bar2     b2b = Bar2();  // x and y not  initialized.
         Bar3     b3a;           // x and y zero initialized.
         Bar3     b3b = Bar3();  // x and y zero initialized.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# member variable initialization; best practice? Is there any benefit to this:
I there any benefit to putting the javascript references in individual pages vs the
Is there any benefit in having never-defined structures in C ? Example in SQLite
Is there going to be any real benefit to me putting indexes onto date
assuming x is an object... Is there any benefit of doing: if (typeof x.foo
Is there any benefit to include the length specifier in a format string for
Is there any benefit (or conversely, cost) to passing a parameter by const value
When using Google app engine is there any benefit to use a CDN if
Is there any performance benefit/technical differences between calling EF SaveChanges() in a foreach loop
Are there any benefits of using the following code? <ul role=list> <li role=listitem></li> <li

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.