1

I am wondering how I can have a pointer const inside a scope, and non const outside.

I read several topics and web pages including newbie question: c++ const to non-const conversion and What's the point of const pointers? The message I heard is "If you need to cast const to non-const, it's dangerous, and your code is likely poorly designed". However, I am not sure how to overcome this :

What I have now :

void compute(const CArray<FOO> &fooArray, CArray<double> &Results)
{
    Results[0] = 0.0 ;
    for(INT_PTR i=0 ; i<fooArray.GetCount()-1 ; ++i)
    {
        const FOO *foo1 = fooArray[i] ;
        DoSomething(foo1) ; //BOOL method which needs a const *FOO as parameter

        const FOO *foo2 = fooArray[i+1] ;
        DoSomething(foo2) ; //BOOL method which needs a const *FOO as parameter

        double res = 0.0 ;
        Compare(foo1, foo2, res) ; //Compare(const FOO *foo1, const FOO *foo2, double &result)
        Results[i+1] = res ;
    }
}

But DoSomething is a quite greedy function, and I'd like avoiding to call it twice on the same object. So I'd like to do something similar than this :

void compute(const CArray<FOO> &fooArray, CArray<double> &Results)
{
    const FOO *foo1 = fooArray[0] ;
    Results[0] = 0.0 ;
    DoSomething(foo1) ; //BOOL method which needs a const *FOO as parameter
    for(INT_PTR i=1 ; i<fooArray.GetCount() ; ++i)
    {
        const FOO *foo2 = fooArray[i] ;
        DoSomething(foo2) ; //BOOL method which needs a const *FOO as parameter

        double res = 0.0 ;
        Compare(foo1, foo2, res) ; //Compare(const FOO *foo1, const FOO *foo2, double &result)
        Results[i] = res ;
        *foo1 = *foo2 ; //what i would like to do, but I can't since *foo1 and *foo2 are const
    }
}

I 'could' remove all the const but as mentionned in the topics I read, there is a good reason they are here : DoSomething and Compare are not supposed to modify *foo1 and *foo2.

Community
  • 1
  • 1
Irminsul
  • 171
  • 9
  • 1
    Make a function that returns const ptr, but inside uses non-const ptr? – lorro Jun 30 '16 at 08:11
  • 1
    You can pass non-const arguments to functions that take const arguments. When a function signature has const arguments it means it promises not to modify them. That is not a problem for modifyable arguments. – kamikaze Jun 30 '16 at 08:23
  • @kamikaze Well that's embarassing.. I guess sometimes I just need a fresh eye on my code. Ty for bringing me back on earth. – Irminsul Jun 30 '16 at 08:48

1 Answers1

2

I 'could' remove all the const

You could indeed, and that would be the solution to your 'problem'. Although making foo1 a pointer to non-const is sufficient.

DoSomething and Compare are not supposed to modify *foo1 and *foo2.

Therefore they should take their parameters either by value, or by const reference (or a pointer to const, if that makes sense for some reason). In either case, it's OK to pass a non-const reference/object (or pointer to non-const) to the function.

pointer const inside a scope, and non const outside

What you want is a pointer that is const within a few function calls, and non-const outside the functions. That is achieved simply as I described: Use a const reference (or a value, which doesn't need to be const) parameter, and pass your non-const variable to the function.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Indeed you are right, I just needed the pointer to be const inside the functions calls and non const outside. Maybe should I edit title ? ty anyway – Irminsul Jun 30 '16 at 08:47