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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:03:22+00:00 2026-05-16T15:03:22+00:00

Look at this code snippet: Size::Size(int iSetWidth, int iSetHeight) :iWidth(iSetWidth), iHeight(iSetHeight) { } Supposedly,

  • 0

Look at this code snippet:

Size::Size(int iSetWidth, int iSetHeight)
:iWidth(iSetWidth),
iHeight(iSetHeight)
{
}

Supposedly, this means the same thing as:

Size::Size(int iSetWidth, int iSetHeight)
{
    iWidth=iSetWidth;
    iHeight=iSetHeight;
}

Why would you use the former or the latter? And what is the name of the former?

  • 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-16T15:03:23+00:00Added an answer on May 16, 2026 at 3:03 pm

    No, they don’t mean exactly the same.

    When a constructor is executed, before entering the code block (the code between the curly braces), it constructs all object data members. What you do in the initializers (the code after the colon and before the curly braces) is to specify which constructors to use for those members. If you don’t specify a constructor for a specific data member, the default constructor will be used.

    So, if you use the initialization list (first example), the right constructors will be used for each member and no additional code is necessary. If you don’t, first the default constructor is used and then the code inside the curly braces is executed.

    In summary:

    1. In your first example, each member is initialised using the appropriate constructor, probably the copy constructor.
    2. In your second example, each member is constructed using the default constructor, and then some additional code is executed to initialise it, probably the assignment operator.

    EDIT: Sorry, forgot to answer your questions in the last line.

    The name of the code between the colon and the curly braces is initialisation list.

    If you know which is the right constructor for a variable or data member, by all means use it. This is the reason why most classes have different constructors instead of just a default constructor. So you are better off using the initialization list.

    The initialisation list is almost never slower than the other technique, and can easily be faster. A well known rule when writing code is “don’t optimize prematurely”, but there is a not so well known counterpart: don’t pessimize prematurely. If you have two options for writing a piece of code and one of them can be better than the other, but does not involve additional work or complexity, use it. In your example there is no difference, since you are using a built-in type (int). But if you were using classes, there would be a difference, so get used to the initialization list.

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

Sidebar

Related Questions

Have a look to this code snippet:- CommonService objcommonService; NetworkCredential myCredentials = new NetworkCredential(144552,
take a look at this example code: public class Comment { private Comment() {
Edit: Of course my real code doesn't look exactly like this. I tried to
I have a code snipet like this: var nbrPrevArt = $(#accordion > div .accordionPanel).size();
Look at this image: alt text http://img139.imageshack.us/img139/4488/picture2ep3.png I know how to add UITableView with
Look at this situation: www.websitea.com displays an img tag with a src attribute of
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I was having a look at this tutorial at Sun on command line I/O.
8 bits representing the number 7 look like this: 00000111 Three bits are set.
I'm not sure how to look for this online... I think they might be

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.