A constructor to a class takes some arguments including a pointer which is assigned to a class variable. The constructor then calls a default constructor, but inside the default constructor the pointer is no longer valid.
some code should illustrate.
here i've puts some prints in to demonstrate, the output is below:
at a 0x1641bb0
a is 642 <-this is the correct value.
at b 0x7fadef8c0ded
Segmentation fault <-trying to access the pointer now segfaults.
since the flow goes from one constructor directly to the other, i dont understand how the class variable oc gets lost. Can anyone explain?
best regards,
Liam
some code should illustrate.
Code:
Feature::Feature(){ cout<<"at b "<<oc<<endl; cout<<"b is "<<oc->geoSphere->getNumVs()<<endl; scoreData=ArrayTools::allocate2DArray<float>(oc->numSteps,oc->geoSphere->getNumVs()); //some stuff } Feature::Feature(float weight,OVASControl* o) : weight(weight),oc(o) { cout<<"at a "<<oc<<endl; cout<<"a is "<<oc->geoSphere->getNumVs()<<endl; Feature(); }
at a 0x1641bb0
a is 642 <-this is the correct value.
at b 0x7fadef8c0ded
Segmentation fault <-trying to access the pointer now segfaults.
since the flow goes from one constructor directly to the other, i dont understand how the class variable oc gets lost. Can anyone explain?
best regards,
Liam
Comment