I am encountering an error where the values of protected members of a base class are changing values between when the parent class constructor is called and when the child constructor is called. A stripped down version of the code is as follows:
Namespace A
{
class Parent
{
public:
Parent (int a, int b, int c, int d);
protected:
std::vector<Eigen::Matrix<float, 3, 3, Eigen::RowMajor>> rmats_;
}
}
A::Parent::Parent (int a, int b, int c, int d) {
rmats_.reserve(3000);
rmats_.clear ();
Eigen::Matrix<float, 3, 3, Eigen::RowMajor> init_Rcam_ = Eigen::Matrix3f::Identity ();
rmats_.push_back(init_Rcam_);
std::cout << "size of rmats is " << rmats_.size() << std::endl;
}
Namespace B
{
class Child : public Parent
{
public:
Child(int a, int b, int c, int d);
}
}
B::Child::Child : A::Parent::Parent(a,b,c,d)
{
std::cout << "size of rmats in the child is " << rmats_.size() << std::endl;
}
When a child object is created, the size in the parent constructor is reporting the expected size of 1, however the output in the child reports that the size of the vector is now 127101589483567331. there are also several other vectors of similar objects in the real code that all report incorrect sizes of vectors including another vector of size 1 changed to size 0 and a vector of size 3 changed to size 668637816.
I have tried with simpler version of the code using vectors of integers and get the expected results however the full code which does nothing additional between the two print cout statements state the size of the vector is changing between the parent constructor and the child constructor. Additionally the code appears to function properly under Linux using gcc, but breaks under windows using visual studio.
Are there any additional hidden steps that are taken during the construction process that would cause this error? Any compiler settings that may cause this type of issue?