dimanche 17 février 2019

Change data in std::vector inside const function

It there a way to change the data stored inside a std::vector inside a const function? See the following code to understand what I want to accomplish:

// class holding properties and data
class Output{
public:
    int * values; 
    std::vector<int> vc;
    mutable std::vector<int> vm;   
    //std::vector<mutable int> vm; something like this,        
};
class Node{
   Output out;
   void test()const{
       // i want to change the "data" of the Output but not the Object 
       out.values[0] = 0;//works: i can change the output data
       out.values = nullptr;//works: compile error, i cant change the pointer
       out.vc[0] = 1; // compile error, not possible :(
       out.vm[0] = 1; // that is what i want
       out.vm.resize(3); // this is now possible, but should be not possible
   }
};

I can use a raw pointer to achieve my goal, but i would prefer a std::vector if this is possible.

Aucun commentaire:

Enregistrer un commentaire