C++ instance variable default initialization
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.
Related videos on Youtube
starcow
Updated on December 24, 2022Comments
-
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 over 10 yearsRe-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 over 3 yearsBoth forms are invalid. You should use
class
with smallc
instead ofClass
. -
Nikos C. over 3 yearsThere 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 writeint 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 over 10 yearsThanks for helping. Tried it. Didn't work. It just kept blinking on and off, even throughout the restarting process. Any other suggestions?
-
Yass over 10 yearsDo you know what Graphics Card your PC is running?
-
Kath over 10 yearsI'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 over 10 yearsNo. How do I find out?
-
Yass over 10 yearsInstall and run this: cpuid.com/softwares/hwmonitor.html
-
Yass over 10 yearsYou could also go into Device Manager from the Control Panel and expand the Display Adapter node. That'll be easier.
-
Kath over 10 yearsThank 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 over 10 yearsThe 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.