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

  • Home
  • SEARCH
  • 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 6144043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:35:58+00:00 2026-05-23T18:35:58+00:00

Parashift explains initialization lists well, but does not explain why an extra copy of

  • 0

Parashift explains initialization lists well, but does not explain why an extra copy of a variable is created before assignment in the ctor body, but no extra copy is created when assigned through an initialization list.
I’ve even come across advice of using ++i instead of i++ because the former avoids creation of a temporary i before assignment. Is it the same for POD’s assigned in a ctor body? A temp variable gets created before the assignment happens?

To put it another way, why does the compiler need to create an extra copy of a variable? Why can’t it just assign the variable directly?
Why?

  • 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-23T18:35:59+00:00Added an answer on May 23, 2026 at 6:35 pm

    Consider the following:

    struct C { 
        C() { /* construct the object */ }
    };
    
    struct X {
        C member;
    
        X() { member = C(); }
    };
    

    The X() constructor is the same as if you had said:

    X() : member() { member = C(); }
    

    First, the C constructor is called to construct the member data member. Then the body of X is executed, a second, temporary C object is created and assigned to member, then the temporary is destroyed.


    Note that this is only the case for types that are initialized automatically. If member was of type int or of a POD class type (as examples), it would be uninitalized when the body of the X constructor is entered.

    For such types it doesn’t really matter from a performance standpoint whether you initialize the data member in the initialization list or assign to the data member in the body of the constructor; the difference is entirely stylistic. Where possible, the initialization list should still be preferred for the sake of consistency.

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

Sidebar

Related Questions

I read the answer in parashift but I need bit details as to why
I know destructor shouldn't not throw exception. http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.13 I have the following code :
In http://www.parashift.com/c++-faq-lite/intrinsic-types.html#faq-26.6 , it is wriiten that Another valid approach would be to define
I know from reading parashift.com that using delete on a pointer frees the memory
I was reading this and unfortunately could not understand in depth why the compiler
// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html class BadConversion : public std::runtime_error { public: BadConversion(std::string const& s) : std::runtime_error(s)
Is there any Java FAQ equivalent of the Parashift C++ FAQ lite ? (Books
I have a question based on this question In the section http://www.parashift.com/c%2B%2B-faq-lite/private-inheritance.html#faq-24.3 the following
I am confused when all the code I find shows returning a reference variable
From http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.14 : Even if the language outlawed const_cast , the only way to

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.