C++ instance variable default initialization

232

You may choose to setup an initialization strategy for the member variable both using designated member initializers as well as member initializer list in constructors. If a given constructor does not initialize a given non-static data member, initialization of that data member will fall back on a designated member initializer, if present.

#include <iostream>

template<std::size_t TAG_N> struct Tag{};

struct Foo {
    int x{42};
      // ^^^^ - designated member initializer (DMI) 
    
    Foo() {}               // Use DMI -> x is 42
    Foo(Tag<0>) {}         // Use DMI -> x is 42
    Foo(Tag<1>) : x(1) {}  // Initialized in mem. init list -> x is 1.
};


int main() {
    std::cout << Foo{}.x << " "
        << Foo(Tag<0>{}).x << " "
        << Foo(Tag<1>{}).x << "\n";
    // 42 42 1
}

Choosing which approach would enter the domain of opinion-based, but favoring consistency is never a bad advice, and [opinionated] many industry experts recommend using designed member initializers in classes which provide more than one constructor, to avoid the pitfall of e.g. adding a new member to the class but forgetting to initialize it in one the overloaded constructors' member initializer lists.

Share:
232

Related videos on Youtube

starcow
Author by

starcow

Updated on December 24, 2022

Comments

  • starcow
    starcow over 1 year

    If I want to assign a default value to an instance variable, which method is to be preferred? Is there a difference?

    Class Foo {
        int x = 0;
    };
    
    Class Foo {
        int x;
        Foo() : x(0) {}
    };
    
    • Sun
      Sun over 10 years
      Re-plug your HDMI and VGA inputs. Make sure they are securely plugged in and use the twisties on the VGA to keep the connection secure.
    • MikeCAT
      MikeCAT over 3 years
      Both forms are invalid. You should use class with small c instead of Class.
    • Nikos C.
      Nikos C. over 3 years
      There is a difference in general, as answered in the answer this is a duplicate of. In this case you're using an int, so it doesn't actually matter. But in general, you can write int x{0} to make both equivalent. As for which is better from a code style perspective is an off-topic question here. But most people seem to agree that inline initialization is preferred, especially since that initialization is skipped if the ctor that gets called uses an initializer for that member. So it doesn't actually have any overhead (there's no "double initialization.")
  • Kath
    Kath over 10 years
    Thanks for helping. Tried it. Didn't work. It just kept blinking on and off, even throughout the restarting process. Any other suggestions?
  • Yass
    Yass over 10 years
    Do you know what Graphics Card your PC is running?
  • Kath
    Kath over 10 years
    I'm thinking it's not the idlling thing, "cause now it just keeps doing it even though I'm doing stuff (I'm posting this from my laptop). I tried Samsung and they were no help.
  • Kath
    Kath over 10 years
    No. How do I find out?
  • Yass
    Yass over 10 years
    Install and run this: cpuid.com/softwares/hwmonitor.html
  • Yass
    Yass over 10 years
    You could also go into Device Manager from the Control Panel and expand the Display Adapter node. That'll be easier.
  • Kath
    Kath over 10 years
    Thank you very much. I couldn't figure out how to expand the display adapter node. I found Device Manager and clicked on the device -- it said it's working fine -- I checked for software updates and it said I'm up to date. I might try the software you suggested, and then call a techie if it doesn't work. Thanks again.
  • Yass
    Yass over 10 years
    The software I suggested is mainly for monitoring temperatures. It shows other information as well, but it isn't designed to actively detect/fix hardware problems.