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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:48:59+00:00 2026-06-08T02:48:59+00:00

Is it good practice when writing C++11 code to set default values for class

  • 0

Is it good practice when writing C++11 code to set default values for class members in the header file of the class?

Or is it better to do this in the constructor of the class?

EDIT:

I mean:

foo.h:

#include <string>

using std::string;

class Foo{
    private:
        string greet = "hello";
    public:
        Foo();
};

VS

foo.cpp (of course with the necessary header file, but without the in-class initialization):

Foo::Foo(){
    greet = "hello";
}

Which one is better and why?

  • 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-08T02:49:00+00:00Added an answer on June 8, 2026 at 2:49 am

    If a class member is always initialized with the same initial value, then you should make the initializer inline, so as to avoid duplication. If the initial value depends on the constructor, then put it in the constructor initializer list. (And never use assignment in the way you did.)

    Example:

    class Foo
    {
        bool done = false;   // always start like this
        int qty;
        Bar * p;
    
    public:
        Foo()                        : qty(0),              p(nullptr)    { }
        Foo(int q, Bar * bp)         : qty(q),              p(bp)         { }
        explicit Foo(char const * s) : qty(std::strlen(s)), p(new Bar(s)) { }
    
        // ...
    };
    

    In this hypothetical example, the member done is always required to start as false, so it’s best to write the initializer inline. The other two members, qty and p, can be initialized differently in each of three different constructors, so they are initialized inside the constructors’ initializer lists.

    A curiosum: Note that providing an inline initializer prevents your class from having a trivial default constructor.

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

Sidebar

Related Questions

Recently I've been writing code similar to this: messagehandler.h: #include message.h class MessageHandler {
When writing a Perl module, is it a good practice to use croak/die inside
Is it good practice to set all text fields to nvarchar(MAX)? if im not
Is there any good practice for this? I wish I could solve the problem
This is my first program using Haskell. I'm writing it to put into practice
I was taught that it's good practice to specify the directory of a file
I am thinking of writing my own code (in order to practice and also
I've seen some people writing Python code by creating one class and then an
Is it good practice to use the private field, or the property, when writing
I am looking for good practice or pattern to write a lib in JS.

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.