c++boostptr-vector

Adding member boost::ptr_vector<>


I have the fallowing classes:

class CpuUsage {
public:
    CpuUsage();
    virtual ~CpuUsage();

    void SetCpuTotalTime(CpuCore _newVal);
    CpuCore GetCpuTotalTimes();

    void AddSingleCoreTime(CpuCore& newval);
private:
    CpuCore total;
    boost::ptr_vector<CpuCore> cpuCores;
};

and

class CpuCore {

public:
    CpuCore();
    CpuCore(int _coreId, long _user, long _nice, long _sysmode,
        long _idle, long _iowait, long _irq, long _softirq, long _steal,
        long _guest);

//all variable declarations...
}

For adding CpuCore objects into the cpuCores vector, should I add a pointer? Or I can copy the value, normaly, like:

void CpuUsage::AddSingleCoreTime(CpuCore _newVal) {
    cpuCores.push_back(_newVal);
}

With the CpuCore *_newVal parameter, I have the following error:
../src/usage/CpuUsage.h:42: error: ‘boost::ptr_vector > CpuUsage::cpuCores’ is private ../src/NodeInfoGather.cpp:73: error: within this context

What the problem of the vector being private here?

Thanks,


Solution

  • You have to add a pointer to ptr_vector. Note that it will take ownership of that pointer, so just doing

    cpuCores.push_back(&_newVal);
    

    might screw things up badly. If you really want it though (which is not clear from your question) you could implement a virtual constructor.