0

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,

Pedro Dusso
  • 2,100
  • 9
  • 34
  • 64
  • You should be adding a pointer, `boost::ptr_vector<>` owns its pointers and the things they point to. Why not using just `std::vector<>`? – K-ballo Nov 09 '11 at 15:30
  • I am using boost ptr vector inspired in this post: http://stackoverflow.com/questions/2693651/c-vector-of-objects-vs-vector-of-pointers-to-new-objects – Pedro Dusso Nov 09 '11 at 15:32
  • 1
    You don't seem to have polymorphism over `CpuCore` at your given code. Yet the code posted is not real, the declaration and definition for `AddSingleCoreTime` are different. – K-ballo Nov 09 '11 at 15:34
  • 2
    What you should or should not write depends on things you havn't told us yet. What are the desired ownership relationships between the involved objects in your design? Are you familiar with the "ownership" concept with respect to resource management in C++? What do you think is the purpose of boost::ptr_vector? – sellibitze Nov 10 '11 at 12:36
  • @sellibitze - thats what im strugling right now. I dont know this ownership concept in the resource mgnt, im sorry. I expected that the vector could hold my elements in a way that when I destroy the class, they are destroyed also and their memory is free. – Pedro Dusso Nov 11 '11 at 12:00
  • @Pedro: ptr_vector stores pointers to *free store allocated* objects. It takes ownership of those objects which means, it feels responsible for deleting them. But your `AddSingleCoreTime` function takes its parameter by value. `_newval` is just a temporary object living in *automatic memory* and it only exists during the function call. So, it's a fairly bad idea to put a pointer to it into a ptr_vector. Of course, you could create a free store-allocated copy and put a pointer to it into the ptr_vector. But that seems ridiculus compared to simply using `std::vector`. – sellibitze Nov 11 '11 at 14:25
  • CpuUsage should not have virtual destructor unless you intent to use it as base class with the possibility of polymorphic deletion. – sellibitze Nov 11 '11 at 14:35

1 Answers1

0

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.

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • yeap, it really worked. But I will revise my architecture, maybe I really do not need the pointer vector - i still have to understand this ownership of c++ – Pedro Dusso Nov 13 '11 at 10:44