mercredi 13 mai 2015

Changing values in polymorphic boost::shared_ptr (inside std::vector)

I have the following base calibration struct:

struct Standard
{
public:
    unsigned long ulCamID;
    std::string sCalibrationModel;
    float fC;
    float fXh;
    float fYh;
    Standard()
    {
        ulCamID = 0;
        fC = fXh = fYh = 0.0;
    }
    virtual ~Standard()
    {

    }
};

And derived structs such as:

struct Aus: public Standard
{
public:
    float fK1;
    float fK2;
    float fK3;
    float fP1;
    float fP2;
    float fB1;
    float fB2;
    Aus()
    {
        fC = fXh = fYh = fK1 = fK2 = fK3 = fP1 = fP2 = fB1 = fB2 = 0.0;
    }
};

Because I do not know at compile how many Calibrations I will need, nor which calibration models, I thought it convenient to put it into a std::vector and use boost::shared_ptr to point to them. I do this like so:

typedef boost::shared_ptr<CalibrationModels::Standard> shr_ptr;
std::vector<shr_ptr> vec;
shr_ptr p(new CalibrationModels::Aus);
vec.push_back(p);
p.reset(new CalibrationModels::Brown);
vec.push_back(p);

This seems to work fine (the debugger reports that the pointers inside the vector point to the derived struct). However, I have difficulties now to access/change the values inside the vector. If I try something like this:

boost::dynamic_pointer_cast<CalibrationModels::Aus>(vec.at(0)).px->fK3 = 1.3221e-9

It tells me that px is private ( ‘boost::shared_ptr::element_type* boost::shared_ptr::px’ is private element_type * px; ).

What is the proper way to access and manipulate the values inside those pointers to derived structs?

Aucun commentaire:

Enregistrer un commentaire