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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:11:53+00:00 2026-06-12T02:11:53+00:00

class A { public: int a; A(int x) { a = x; } };

  • 0
class A {
   public:
   int a;
   A(int x)
   {
      a = x;
   }
};

OR

class B {
       public:
       int b;
       B(int x):b(x){}
};

Which one would initialize the object faster ? Or will same code be generated ultimately for both and the time taken to initialize will remain the same ? Or does it depend on the compiler ?

  • 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-12T02:11:55+00:00Added an answer on June 12, 2026 at 2:11 am

    For POD members, including int, they will be the same, because the member won’t be constructed twice.

    For types with a default constructor, the second will be faster, because the first option is equivalent to:

    A(B x) : a()
    {
       a = x;
    }
    

    if a is a member of type B (with a default constructor).

    Note that there are situations where you must use initializer lists: const members, reference members, members of a class-type that don’t have a default constructor.

    POD-member:

    No initializer list:

       A(int x)
    001F1430  push        ebp  
    001F1431  mov         ebp,esp  
    001F1433  sub         esp,0CCh  
    001F1439  push        ebx  
    001F143A  push        esi  
    001F143B  push        edi  
    001F143C  push        ecx  
    001F143D  lea         edi,[ebp-0CCh]  
    001F1443  mov         ecx,33h  
    001F1448  mov         eax,0CCCCCCCCh  
    001F144D  rep stos    dword ptr es:[edi]  
    001F144F  pop         ecx  
    001F1450  mov         dword ptr [ebp-8],ecx  
       {
          a = x;
    001F1453  mov         eax,dword ptr [this]  
    001F1456  mov         ecx,dword ptr [x]  
    001F1459  mov         dword ptr [eax],ecx  
       }
    

    Initializer list:

       A(int x) : a(x)
    00E71430  push        ebp  
    00E71431  mov         ebp,esp  
    00E71433  sub         esp,0CCh  
    00E71439  push        ebx  
    00E7143A  push        esi  
    00E7143B  push        edi  
    00E7143C  push        ecx  
    00E7143D  lea         edi,[ebp-0CCh]  
    00E71443  mov         ecx,33h  
    00E71448  mov         eax,0CCCCCCCCh  
    00E7144D  rep stos    dword ptr es:[edi]  
    00E7144F  pop         ecx  
    00E71450  mov         dword ptr [ebp-8],ecx  
    00E71453  mov         eax,dword ptr [this]  
    00E71456  mov         ecx,dword ptr [x]  
    00E71459  mov         dword ptr [eax],ecx  
    

    i.e. identical, & that’s because a isn’t initialized before entry to the constructor body.

    Non-POD member:

    No initializer list:

    00EB1A80  push        ebp  
    00EB1A81  mov         ebp,esp  
    00EB1A83  sub         esp,0CCh  
    00EB1A89  push        ebx  
    00EB1A8A  push        esi  
    00EB1A8B  push        edi  
    00EB1A8C  push        ecx  
    00EB1A8D  lea         edi,[ebp-0CCh]  
    00EB1A93  mov         ecx,33h  
    00EB1A98  mov         eax,0CCCCCCCCh  
    00EB1A9D  rep stos    dword ptr es:[edi]  
    00EB1A9F  pop         ecx  
    00EB1AA0  mov         dword ptr [ebp-8],ecx  
    00EB1AA3  mov         ecx,dword ptr [this]  
    00EB1AA6  call        B::B (0EB11D6h)  
       {
           a = b;
       }
    00EB1AAB  mov         eax,dword ptr [this]  
    00EB1AAE  pop         edi  
    00EB1AAF  pop         esi  
    00EB1AB0  pop         ebx  
    00EB1AB1  add         esp,0CCh  
    00EB1AB7  cmp         ebp,esp  
    00EB1AB9  call        @ILT+315(__RTC_CheckEsp) (0EB1140h)  
    00EB1ABE  mov         esp,ebp  
    00EB1AC0  pop         ebp  
    00EB1AC1  ret         4  
    

    Initializer List:

    A(B b) : a(b)
       {
    00841650  push        ebp  
    00841651  mov         ebp,esp  
    00841653  sub         esp,0CCh  
    00841659  push        ebx  
    0084165A  push        esi  
    0084165B  push        edi  
    0084165C  push        ecx  
    0084165D  lea         edi,[ebp-0CCh]  
    00841663  mov         ecx,33h  
    00841668  mov         eax,0CCCCCCCCh  
    0084166D  rep stos    dword ptr es:[edi]  
    0084166F  pop         ecx  
    00841670  mov         dword ptr [ebp-8],ecx  
       }
    

    i.e. initializer list option is better.

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

Sidebar

Related Questions

public class Fruit { // choose one public int Id { get; set; }
So I want a C++ class to contain for example one public function: int
My class: public class Foo { public int A { get; set; } public
Given the class: public class Item { [Key] public int ID { get; set;
I have a class: public class LevelInfo { public int Id { get; set;
Consider the following class public class Class1 { public int A { get; set;
Say I have this class: public class Account { public int AccountID { get;
I have the following class: public class Account { public int AccountID { get;
I have the following class: public class Day { public int Date { get;
Except from my Rect-class: public class Rect { public int x; public int y;

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.