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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:27:00+00:00 2026-05-27T06:27:00+00:00

Constructors should initialize all its member objects through initializer list if possible. It is

  • 0

Constructors should initialize all its member objects through
initializer list if possible. It is more efficient than building the
constructors via assignment inside the constructor body.

Could someone explain, why it is more efficient to use the initializer list with the help of an example?

  • 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-27T06:27:01+00:00Added an answer on May 27, 2026 at 6:27 am

    Consider this program:

    #include <iostream>
    
    struct A {
      A() { std::cout << "A::A()\n"; }
      A(int) { std::cout << "A::(int)\n"; }
      void operator=(const A&) { std::cout << "A::operator=(const A&)\n"; }
    };
    
    struct C1 {
      A a;
      C1(int i) { 
        a = i;
      }
    };
    
    struct C2 {
      A a;
      C2(int i)  : a(i) {}
    };
    
    int main() {
      std::cout << "How expesive is it to create a C1?\n";
      { C1 c1(7); }
      std::cout << "How expensive is it to create a C2?\n";
      { C2 c2(7); }
    }
    

    On my system (Ubuntu 11.10, g++ 4.6.1), the program produces this output:

    How expesive is it to create a C1?
    A::A()
    A::(int)
    A::operator=(const A&)
    How expensive is it to create a C2?
    A::(int)
    

    Now, consider why it is doing that. In the first case, C1::C1(int), a must be default-constructed before C1‘s constructor can be invoked. Then it is must assigned to via operator=. In my trivial example, there is no int assignment operator available, so we have to construct an A out of an int. Thus, the cost of not using an initializer is: one default constructor, one int constructor, and one assignment operator.

    In the second case, C2::C2(int), only the int constructor is invoked. Whatever the cost of a default A constructor might be, clearly the cost of C2:C2(int) is not greater than the cost of C1::C1(int).


    Or, consider this alternative. Suppose that we add the following member to A:

    void operator=(int) { std::cout << "A::operator=(int)\n"; }
    

    Then the output would read:

    How expesive is it to create a C1?
    A::A()
    A::operator=(int)
    How expensive is it to create a C2?
    A::(int)
    

    Now is is impossible to say generally which form is more efficient. In your specific class, is the cost of a default constructor plus the cost of an assignment more expensive than a non-default constructor? If so, then the initialization list is more efficient. Otherwise it isn’t.

    Most classes that I’ve ever written would be more efficiently initialized in an init list. But, that is a rule-of-thumb, and may not be true for every possible case.

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

Sidebar

Related Questions

Why are copy constructors unnecessary for immutable objects? Please explain this for me.
I have an ASP.NET webform where I initialize an array with a list of
I am using a FindFile routine to search through all files in a directory.
Is there a way in Python, to have more than one constructor or more
should i use Classname() or __construct() as constructor in CodeIgniter? both work, which should
Why in C# 3.0, when we overload constructor of a specified class, we should
Are constructors allowed to throw exceptions?
I find that my constructors are starting to look like this: public MyClass(Container con,
Suppose we want two constructors for a class representing complex numbers: Complex (double re,
I have two copy constructors Foo(Foo &obj){ } Foo(Foo *obj){ } When will the

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.