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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:11:24+00:00 2026-06-03T15:11:24+00:00

I have seen lots of codes where the coders define an init() function for

  • 0

I have seen lots of codes where the coders define an init() function for classes and call it the first thing after creating the instance.

Is there any harm or limitation of doing all the initializations in Constructor?

  • 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-03T15:11:26+00:00Added an answer on June 3, 2026 at 3:11 pm

    It is a design pattern that has to do with exceptions thrown from inside an object constructor.

    In C++ if an exception is thrown from inside an object costructor then that object is considered as not-constructed at all, by the language runtime.
    As a consequence the object destructor won’t be called when the object goes out of scope.

    This means that if you had code like this inside your constructor:

    int *p1 = new int;
    int *p2 = new int;
    

    and code like this in your destructor:

    delete p1;
    delete p2;
    

    and the initialization of p2 inside the constructor fails due to no more memory available, then a bad_alloc exception is thrown by the new operator.
    At that point your object is not fully constructred, even if the memory for p1 has been allocated correctly.
    If this happens the destructor won’t be called and you are leaking p1.

    So the more code you place inside the constructor, the more likely an error will occur leading to potential memory leaks.

    That’s the main reason for that design choice, which isn’t too mad after all.

    More on this on Herb Sutter’s blog: Constructors exceptions in C++

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

Sidebar

Related Questions

Not looking really for a book. I have seen lots of references and links
I have seen lots of ways of running Perl code or scripts, with different
I've seen lots of posts on SE relating to this, but none have answered
I have seen lots of similar queries to this, but am struggling to get
I have seen lots of similar questions but nothing is working for me. I
I have seen lots of class names containg word 'Nugget' in one css file.
So, this question has been asked lots, and i have seen many different answers,
Have seen some similar questions: What is the difference between a JavaBean and a
I have seen this question about deploying to WebSphere using the WAS ant tasks.
I have seen two commonly used techniques for adding the directory of the file

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.