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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:18:12+00:00 2026-05-17T16:18:12+00:00

I want to ask you for your best practices regarding constructors in C++. I

  • 0

I want to ask you for your best practices regarding constructors in C++. I am not quite sure what I should do in a constructor and what not.

Should I only use it for attribute initializations, calling parent constructors etc.?
Or might I even put more complex functions into them like reading and parsing configuration data, setting up external libraries a.s.o.

Or should I write special functions for this? Resp. init() / cleanup()?

What are the PRO’s and CON’s here?

I figured out yet that for example I can get rid of shared pointers when using init() and cleanup(). I can create the objects on the stack as class attributes and initialize it later while it is already constructed.

If I handle it in the constructor I need to instantiate it during runtime. Then I need a pointer.

I really don’t know how to decide.

Maybe you can help me out?

  • 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-17T16:18:12+00:00Added an answer on May 17, 2026 at 4:18 pm

    Complex logic and constructor do not always mix well, and there are strong proponents against doing heavy work in a constructor (with reasons).

    The cardinal rule is that the constructor should yield a fully usable object.

    class Vector
    {
    public:
      Vector(): mSize(10), mData(new int[mSize]) {}
    private:
      size_t mSize;
      int mData[];
    };
    

    It does not mean a fully initialized object, you may defer some initialization (think lazy) as long as the user does not have to think about it.

    class Vector
    {
    public:
      Vector(): mSize(0), mData(0) {}
    
      // first call to access element should grab memory
    
    private:
      size_t mSize;
      int mData[];
    };
    

    If there is heavy work to be done, you might choose to proceed with a builder method, that will do the heavy work prior to calling the constructor. For example, imagine retrieving settings from a database and building a setting object.

    // in the constructor
    Setting::Setting()
    {
      // connect
      // retrieve settings
      // close connection (wait, you used RAII right ?)
      // initialize object
    }
    
    // Builder method
    Setting Setting::Build()
    {
      // connect
      // retrieve settings
    
      Setting setting;
      // initialize object
      return setting;
    }
    

    This builder method is useful if postponing the construction of the object yields a significant benefit. From example if the objects grab a lot of memory, postponing the memory acquisition after tasks that are likely to fail may not be a bad idea.

    This builder method implies Private constructor and Public (or friend) Builder. Note that having a Private constructor imposes a number of restrictions on the usages that can be done of a class (cannot be stored in STL containers, for example) so you might need to merge in other patterns. Which is why this method should only be used in exceptional circumstances.

    You might wish to consider how to test such entities too, if you depend on an external thing (file / DB), think about Dependency Injection, it really helps with Unit Testing.

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

Sidebar

Related Questions

I am not sure the best way to ask this question. I am new
I just want to ask for your experience. I'm designing a public website, using
i want ask some question about asp.net mvc Is static constructor will init every
want to ask user to input something but not want to wait forever. There
I want to ask my application only for 4.0 and 4.3 screens mobile. When
not the best with Javascript so thought i'd ask where i'm going wrong. as
wanna ask for your opinion. What would be the best object (Array, List<>, Collection,...)
I want to ask for your prefered way to test Java EE code? I
I want to ask your opinion about one specific aspect of endless Qt vs
I just want to ask for your opinion on how to achieve the 'Slide

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.