This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Inherited member( void )const - const to be or not to be
- From: Claudio Bley <bley at CS dot uni-magdeburg dot de>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 21 Sep 2004 15:34:38 +0200
- Subject: Re: Inherited member( void )const - const to be or not to be
- References: <chheqb$k1i$1@sea.gmane.org>
On Mon, Sep 06, 2004 at 12:40:10PM +0200, SVisor wrote:
> Hi,
Hi.
> If this is wrong list, please point me to a better one.
>
> I have following code. If CONST (as described in code) is defined it
> will not compile (unless I do a C cast to CProvider* in
> CInherited::getProvider(void)const). *I* claim that it should compile
> even if CONST is defined. As the "const" only tells the compiler that
> the function will not change any members of the class, it says nothing
> about the return value. GCC 3.4.1 20040625 wants to see "this" as
> constant in the function.
>
> What is your opinion?
>
> Am I getting myself into trouble with the CInherited class if I use C
> cast (with C++ static/dynamic_cast it fails to compile) to make it compile?
>
> virtual CProvider* getProvider( void )CONST{ return this; }
This is forbidden. You can't assign a constant pointer (and "this" is const
in the body of that function) to a non-const pointer:
const int *pc;
int *p;
p = pc;
*p = 5;