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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:01:20+00:00 2026-06-18T07:01:20+00:00

There are at least two ways to initialize a class in C++. (1) Initializer

  • 0

There are at least two ways to initialize a class in C++.

(1) Initializer List

struct C
{
  int i;
  C() : i(0) {}
};

(2) Initializer Method

struct D
{
  int i;
  C() { init(); }
  void init() {
    i = 0;
  }
};

I need to re-init objects of my class from time to time. With the second solution, I can simply call obj.init(). With the first solution, I would either have to add an init() function which essentially duplicates the initializer list effect or use obj = C().

Is there a more-or-less consensus on what variant is better here? Is there a disadvantage to using an initializer method (except the possible loss of performance as mentioned in the C++ FAQ).

  • 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-18T07:01:21+00:00Added an answer on June 18, 2026 at 7:01 am

    When creating an array (using vector, or allocating dynamically using new) you will have to explicitly call init on each of its members while using a constructor, it will automatically be called for all elements.

    I prefer placing basic initialization into the constructor and more complex logic into an init method. In my opinion a constructor should not perform any complex operations.

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

Sidebar

Related Questions

I can use at least two basic ways to access a protected class method
There are at least two ways of doing LEFT OUTER JOIN in LINQ class
From previous post, I learnt that for there are two ways, at least, to
Well, there are at least two low-level ways of determining whether a given number
There are at least two ways that I know of to write a Symbian
There appears to be at least two ways to add a default constraint using
As most people know there are at least two (easy) ways to findout if
I find that there are two ways at least to create a record in
There are at least two ways to write to a file in python: f
I'm interested in opinions on design. There are at least two ways to locate

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.