Re: gcc 4 const confusion
Re: gcc 4 const confusion
- Subject: Re: gcc 4 const confusion
- From: David Leimbach <email@hidden>
- Date: Tue, 31 May 2005 10:10:55 -0700
The easiest way to "get const right" is to read your type declarations
from right to left.
int a; [a is an int]
int * b; [ b is a pointer to an int ]
int const a; [ a is a const int]
int * const a; [ a is a const pointer to an int]
int const * a; [ a is a pointer to a const int]
int const * const a; [ a is a const pointer to a const int]
The rule is the CV qualifier [const or volatile] always applies to the
thing to its left. Unless there isn't one, then it applies to the
right.
Thats where you get
const int a; [ a is a const int]
const int * const a; [ a is a const pointer to a const int].
I try my best to discourage people from using "const int" in place of
"int const" but it is basically in vain and you'll have to learn to
read it both ways.
Dave
On 5/30/05, Alexey Proskuryakov <email@hidden> wrote:
> On 28.05.2005 04:16, "Scott Lahteine" <email@hidden> wrote:
>
> > virtual void InsertCopyBefore(UInt16 index, const T** elemPtr,
> > UInt16 count=1)
> <...>
> > inline void InsertCopyBefore(UInt16 index, const TObjectArray<T>
> > &src) { InsertCopyBefore(index, src.m_array, src.Size()); }
> >
> >
> > The last method is the one that gets the error in gcc4:
> >
> > TArray.h:418: error: invalid conversion from 'FPChordGroup** const'
> > to 'const FPChordGroup**'
> > TArray.h:418: error: initializing argument 2 of 'void
> > TObjectArray<T>::InsertCopyBefore(UInt16, const T**, UInt16) [with T
> > = FPChordGroup]'
> >
> >
> > But I'm probably misunderstanding the concept of constness in some way.
>
> The issue here is that a pointer to a const object is different from a
> const pointer to an object (or from a const pointer to a const object). You
> are trying to pass a const pointer as a pointer to a const object, and the
> compiler rightfully complains.
>
> > What changes can I make to my code so that this error won't occur and
> > yet maintain the const-ness of that argument?
>
> Without knowing more about the architecture of your application, I'd say
> that the best approach would be to use STL rather than a custom container. A
> few days ago, the options were discussed on this list (search for a message
> from Howard Hinnant).
>
> As an immediate fix, you may try to declare m_array mutable.
>
> - WBR, Alexey Proskuryakov
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden