Re: Modifying through pointer-to-const

From: Dan Bloomquist (EXTRApublic20_at_lakeweb.com)
Date: 04/20/04


Date: Mon, 19 Apr 2004 22:00:11 GMT


Russell Hanneken wrote:
> Dave wrote:
>
>>In the code below, I am able to modify data through the pointer-to-const
>>DynGapRec parameter. The last two lines show this.
>>Is there any circumstance where this should be legal, or
>>do I definitely have a compiler bug (VC++ 7.1)?
>>
>>void CInstrProcEngine::AggregateDynamicGapRecords(const CAccount*
>> iAcct, const SingleInstr* pInstr,
>> const int* evPoints,
>> const CGapInt* pGap,
>> const DynGap* DynGapRec,
>> CDate forecastDate,
>> int RateType,
>> int startPd,
>> int endPd)
>>{
>
> [. . .]
>
>> DynGaRec->MvPeriod[index] = currentEvPt; // ILLEGAL!!!
>> DynGapRec->GapInteval[index] = pGap->i_GapEndMt[gapRecCnt]; //
>>ILLEGAL!!!
>
>
> Dave,
>
> This would be legal if MvPeriod and GapInteval are pointers to non-const
> data. Assuming this is the case, you're changing the values of data being
> pointed to, not the values of the pointers per se. Since the pointers are
> members of DynGap, and the data being pointed to are not, the assignments
> don't affect the state of DynGapRec (as far as the compiler is concerned,
> anyway).

I've tested in 7.1 and this is the case. If the member is declared
const, then the compiler will balk.

Best, Dan.

-- 
http://lakeweb.net
http://ReserveAnalyst.com
No EXTRA stuff for email.


Relevant Pages

  • Re: Modifying through pointer-to-const
    ... Dave wrote: ... This would be legal if MvPeriod and GapInteval are pointers to non-const ... don't affect the state of DynGapRec (as far as the compiler is concerned, ... and providing non-const member functions to alter the data they ...
    (comp.lang.cpp)
  • Re: Storing a tree in a std::list<>
    ... Dave wrote: ... > tree where a vertex contain pointers to its parent / children. ... that the standard actually requires it. ... adding/removing elements do not ...
    (comp.lang.cpp)
  • Re: delay_check and check_subject problem
    ... Dave wrote: ... > Any pointers as to where i should be looking and what for would be most ... I am not clued up on programming so dont really want to go ... is too lazy to trim his article, and it's wasting the time of all readers. ...
    (comp.mail.sendmail)
  • Re: Is this good style of C++?
    ... > Dave O'Hearn wrote: ... >> does not solve the object ownership problem. ... This eliminates dangling pointers, ... a reference counted smart pointer, ...
    (comp.lang.cpp)