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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:28:32+00:00 2026-06-05T07:28:32+00:00

Possible Duplicate: Benefits of Initialization lists I’ve been learning C++ for the past few

  • 0

Possible Duplicate:
Benefits of Initialization lists

I’ve been learning C++ for the past few days now and I’m seeing two formats where I can’t determine advantages/disadvantages of the two. Hope someone can help me here.

The first one has variables being initialized with var(value)

class Foo
{
    public:
        Foo(): itsVar1(2), itsVar2(345){}

    private:
        int itsVar1;
        int itsVar2;      
};

The second is initialized with the assignment operator var = value.

class Foo
{
    public:
        Foo()
        {
            itsVar1 = 2;
            itsVar2 = 345;
        }

    private:
        int itsVar1;
        int itsVar2;
}; 

Is there an advantage to one over the other? Is it personal preference?
The first style(?) looks more confusing to me. It looks like you’re calling a method and passing in that value. Looks very implicit; whereas, the second is very explicit and as someone coming from Python “explicit is better than implicit” I prefer the second method. What am I missing?

  • 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-05T07:28:33+00:00Added an answer on June 5, 2026 at 7:28 am

    Always use initializer lists (the first style) whenever you can. The major good reasons for doing so are:

    1. Not using initializer lists makes it harder to guarantee exception safety. Using RAII classes for your class member variables, along with initializer lists, makes it trivial to guarantee that your constructors won’t leak resources when faced with exceptions thrown by the constructors of the member variables.

    2. Assignments are no better performing than initializer lists. If the member variable types are nontrivial, the assignments will perform worse in fact. With initializers, member variables will be constructed in place and there will be no default construction happening. Using assignments lets all the member variables become default constructed first.

    3. Initializer lists are the only way to assign values to reference and const member variables.

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

Sidebar

Related Questions

Possible Duplicate: Benefits of Initialization lists I was wondering if there was an advantage
Possible Duplicate: C# member variable initialization; best practice? Is there any benefit to this:
Possible Duplicate: Benefits of omitting closing body and html tags? If you see google.com
Possible Duplicate: MVC versus WebForms Was curious what the main benefits of going MVC
Possible Duplicate: Why not use tables for layout in HTML? These days, I am
Possible Duplicate: SpecFlow/BDD for Unit Tests? Over the last few years I have worked
Possible Duplicate: Benefits of inline functions in C++? I have a confusion regarding the
Possible Duplicate: How do you like your primary keys? I'm aware of the benefits
Possible Duplicate: Why to use Interfaces ? Multiple Inheritance vs Interfaces ? Benefits of
Possible Duplicate: C++: “std::endl” vs “\n” In Accelerated C++ , two things are mentioned:

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.