Let's consider definition:
struct ClassWithMember
{
int myIntMember = 10;
}
I'd like to get myIntMember
's default value, but do not create another instance of the class
// IMPOSSIBLE int myInt = ClassWithMember::myIntMember;
// MUST AVOID int myInt = ClassWithMember().myIntMember;
I know workaround, but dislike it:
struct ClassWithMember
{
static const int myIntMember_DEFAULT = 10;
int myIntMember = myIntMember_DEFAULT;
}
int myInt = ClassWithMember::myIntMember_DEFAULT;
Because it needs extra line. And I cannot define inline static pointers like static const int *const INTEGER11_DEFAULT = 0x100088855;
, such pointers must be defined in .cpp
file, in .hpp
is only declaration. Many of my classes are header-only, so create excess .cpp
for this value isn't good idea.
Here is the similar question for C#