-2

Consider this:

struct Foo {
    std::vector<double> x;
    mutable double* xp;
    void foo () const { xp = &(x[0]); }    
};

This wont compile with because of error: invalid conversion from 'const double*' to 'double*' [-fpermissive]

The fix is rather simple:

struct Foo {
    mutable std::vector<double> x;
    mutable double* xp;
    void foo () const { xp = &(x[0]); }    
};

But what if I do not want to make the vector mutable? What is the best way to get a pointer to non-const from a const vector in this case?

The problem I am trying to solve is the following:

I have some code that looks like this (sorry cant post a complete code):

struct Foo {
    mutable DataSource data;                                   
    void load(int index) const {  data->GetEntry(index); }   
    void getX()          const { return data->element->x; }        
};

Where DataSource I cannot change and its GetEntry reads from a file to update its element. Reading from the file in this way is rather slow, thus I would like to change it to

struct Foo {
   mutable DataSource data;
   std::vector<DataSource::Element> elements;
   DataSource::Element* current;
   void loadAll() {  /*... read all elements into the vector ...*/ }
   void load(int index) const { current = &(elements[index]); }   
   void getX()          const { return current->x; }        
};      

because that is what I can do without breaking (lots of) existing code. I could drop the constness at all which would be maybe a better design, but if possible I want to avoid that, because then I have to fix things in other places.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 6
    **Why** do you want a pointer to non-`const`? What's the real problem? – Cheers and hth. - Alf Jul 05 '16 at 12:43
  • @Cheersandhth.-Alf so that I can assign it to `xp` – 463035818_is_not_an_ai Jul 05 '16 at 12:43
  • 6
    There is no "best way". This [looks like an XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the real problem you're trying to solve. No, not the one that's being asked here, but a problem whose solution you believe involves mutable class members, and you're asking about. – Sam Varshavchik Jul 05 '16 at 12:43
  • @SamVarshavchik I will add some more info – 463035818_is_not_an_ai Jul 05 '16 at 12:44
  • 2
    Keep in mind that you can also have a mutable pointer to a const object, i.e. `mutable const double *xp; void foo() const { xp = &x[0]; }` is perfectly valid, if you never modify *xp or take a non-const reference to *xp. – majk Jul 05 '16 at 12:59
  • 1
    Why is `load()` declared as a `const` function in the first place, when its sole purpose is apparently changing the object's state? – ComicSansMS Jul 05 '16 at 13:18
  • 2
    Probably @majk's comment is the solution you're looking for. I'll just note that `void getX() const { return current->x; }` won't compile. So none of this is **real code**, and when you post a question with just vague descriptions of the problem, chances of getting an optimal answer are dismal. – Cheers and hth. - Alf Jul 05 '16 at 14:22

1 Answers1

-1

An old trick we used for compilers which didn't had mutable implemented some time ago was to const_cast away the constness of the this object, like:

void foo () const { xp = &( (const_cast<Foo*>(this)->x)[0]); }    

Works perfectly for your situation too.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • 2
    (Not the downvoter) I agree that this "works" in that it makes the compiler stop complaining, but there needs to be some mention that OP is walking into undefined behavior territory. The fact that they are trying to do this at all indicates a design flaw that needs to be addressed. – AndyG Jul 05 '16 at 13:09
  • 2
    From what little the OP wrote, it looks like they have some serious design flaws in their interface with regards to const-correctness. This will really just make the code compile, but it does not address any of the real issues with that code. – ComicSansMS Jul 05 '16 at 13:11
  • @fritzone: Is this something you were *trying* to accomplish? From the sounds of it, you intentionally misled OP? – AndyG Jul 05 '16 at 13:19
  • @AndyG Not at all, it also came as a surprise to me. And I definitely didn't wanted to mislead anyone with the answer, I just remembered and old technique we used some time ago in the age of non-entirely-standard-compliant-compilers we used to "hack" situations like this. Indeed, the const correctness of the situation has something to desire, but without a deeper knowledge of the entire situation this is the closest I could get to compile the code. – Ferenc Deak Jul 05 '16 at 13:27
  • Casting away const from an object that was not originally declared as non-const and writing to it is undefined behaviour - don't go there. – Jesper Juhl Jul 05 '16 at 13:52
  • 1
    if I may comment... the question was asking how to make it compile (if required quick and dirty). I am aware that the design has a serious flaw, but at the current stage I have to live with that and just get it running somehow. Thus this answer was acceptable for me. – 463035818_is_not_an_ai Jul 05 '16 at 15:37