I have had the following structure:
struct MyStruct
{
struct Displacement
{
bool isWrapped;
int steps;
};
int initialDuration;
int currentDuration;
Displacement displacement1;
Displacement displacement2;
};
And I was using it like this:
MyStruct obj{2, 2};
auto shared = std::make_shared<MyStruct>(obj);
The problem was that I needed to duplicate first two arguments as initialDuration
and currentDuration
should be the same when a new object is created. Therefore I have created a CTOR:
struct MyStruct
{
struct Displacement
{
bool isWrapped;
int steps;
};
MyStruct(int duration)
: initialDuration(duration)
, currentDuration(duration)
{
}
int initialDuration;
int currentDuration;
Displacement displacement1;
Displacement displacement2;
};
and then used like this:
auto shared = std::make_shared<MyStruct>(2);
The unexpected thing 1 is: both Displacement
members of MyStruct
have been initialized with garbage. For one the bool
member of true
, for the other one - false
and int
s were some arbitrary numbers.
So though maybe I need to define CTOR for Displacement
too. Defined like this:
struct Displacement
{
Displacement() : isWrapped(false), steps(0) {}
bool isWrapped;
int steps;
};
The unexpected thing 2 is: that somewhere else
MyStruct::Displacement d {false, 0};
started to not compile. I don't know aggregate initialization or list-initialization stopped working for a POD struct when I have defined the default CTOR.
Please explain the reasons of those 2 behaviours.