public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Inherited member( void )const - const to be or not to be
@ 2004-09-07 16:40 lrtaylor
  0 siblings, 0 replies; 7+ messages in thread
From: lrtaylor @ 2004-09-07 16:40 UTC (permalink / raw)
  To: svisor, gcc-help

Basically, you can't have a const function returning a non-const pointer
or reference to that object, because that would then allow you to call a
non-const function on the object, violating the fact that you declared
the first function as const.  If you want to get a non-const pointer to
the object, then you can't declare your getProvider function as const,
because it really isn't, even though it is not changing any members of
the class.

Thanks,
Lyle


-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of SVisor
Sent: Tuesday, September 07, 2004 10:35 AM
To: gcc-help@gcc.gnu.org
Subject: Re: Inherited member( void )const - const to be or not to be


Hi and thanks for the reply, but I do not agree...

...
> Don't use _CONST ... that's a reserved symbol.

Well that was news. Anyway it was just "dummy" code.

...
> All symbols starting with underscore followed by a capital letter are 
> reserved.  All symbols with two underscores in a row anywhere are
reserved.

name__more is not reserved. __name is reserved. Usage of _Name is not 
recomended, but what I know not reserved. At least thats what I have 
been taught. No one has _ever_ shown proof of anything else (heck no one

has shown me proof of what Ive learned, I just have accepted it ;-). Do 
you have any link to a list of which combinations are reserved and which

are not?

> I've fixed your code, works now just fine.  Compiled either way.

NO! You _broke_ it! Now you return a pointer to constant, and thats 
_NOT_ what I wanted. I wanted to hint the compiler that a function does 
not change members, but I can still change members (and call non 
constant member-functions) using the returned pointer (and its legal 
C++). The question was more about cast from "this", like this:

virtual CProvider* getProvider( void )const{ return (CProvider*)this; }

It will compile and provide sane working code, but its C (not C++) 
styled. And the cast is not _safe_: The code would compile even if the 
class would not inherit from CProvider.

...
> BTW:  using the -DCONST=const is preferred.  So much so, that you
should 

I do not agree as thats not portable across all compilers. While my 
construct is (well 99.9% of them at least). The code was just dummy code

to show a point, and make it easy to test. You usually know if something

is const or not :-).

// Jarmo

-- 

> just have const in the code.  Const correctness should be worked in
from 
> the beginning.
> 
> HTH,
> --Eljay
> 
> - - - - - - - - -
> // Compile #1:  g++ -DCONST=const foo.cpp
> // Compile #2:  g++ -DCONST= foo.cpp
> #include <cstdio>
> 
> class CProvider
> {
> protected:
>     const char* str;
> public:
>     CProvider(const char* s)
>     : str(s)
>     {
>         printf("CProvider(%s)\n",str);
>     }
> 
>     void echo() CONST
>     {
>         printf("CProvider(%s)::echo()\n", str);
>     }
> };
> 
> 
> class CAbstract
> {
> public:
>     CAbstract()
>     {
>         printf("CAbstract\n");
>     }
> 
>     virtual ~CAbstract()
>     { }
> 
>     virtual CProvider CONST* getProvider() CONST = 0;
> };
> 
> 
> class CAllocated : public CAbstract
> {
> protected:
>     CProvider* ptr;
> 
> public:
>     CAllocated()
>     {
>         printf("CAllocated\n");
>         ptr = new CProvider("Allocated");
>     }
> 
>     virtual CProvider CONST* getProvider() CONST
>     {
>         return ptr;
>     }
> };
> 
> 
> class CInherited : public CAbstract, public CProvider
> {
> public:
>     CInherited()
>     : CProvider("Inherited")
>     {
>         printf("CInherited\n");
>     }
> 
>     virtual CProvider CONST* getProvider() CONST
>     {
>         return this;
>     }
> };
> 
> 
> int main()
> {
>     CAllocated tmp1;
>     CInherited tmp2;
> 
>     tmp1.getProvider()->echo();
>     tmp2.getProvider()->echo();
> 
>     CAbstract* pTmp1 = &tmp1;
>     CAbstract* pTmp2 = &tmp2;
>     pTmp1->getProvider()->echo();
>     pTmp2->getProvider()->echo();
> }
> 
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inherited member( void )const - const to be or not to be
  2004-09-21 13:34 ` Claudio Bley
@ 2004-09-21 13:41   ` Claudio Bley
  0 siblings, 0 replies; 7+ messages in thread
From: Claudio Bley @ 2004-09-21 13:41 UTC (permalink / raw)
  To: gcc-help


Sorry, for that message folks, I didn't intend to send it at all,
actually. Just hit the wrong key.

-- 
Claudio

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inherited member( void )const - const to be or not to be
  2004-09-06 11:01 SVisor
  2004-09-07 12:58 ` Eljay Love-Jensen
@ 2004-09-21 13:34 ` Claudio Bley
  2004-09-21 13:41   ` Claudio Bley
  1 sibling, 1 reply; 7+ messages in thread
From: Claudio Bley @ 2004-09-21 13:34 UTC (permalink / raw)
  To: gcc-help

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;
 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inherited member( void )const - const to be or not to be
  2004-09-07 16:35   ` SVisor
@ 2004-09-07 17:10     ` Eljay Love-Jensen
  0 siblings, 0 replies; 7+ messages in thread
From: Eljay Love-Jensen @ 2004-09-07 17:10 UTC (permalink / raw)
  To: svisor, gcc-help

Hi Jarmo,

 >name__more is not reserved.

Yes it is.  ISO 14882 section 17.4.3.1.2.

 >Usage of _Name is not recomended, but what I know not reserved.

Yes it is.  ISO 14882 section 17.4.3.1.2.

 >Do you have any link to a list of which combinations are reserved and 
which are not?

Only by citing the section in the ISO 14882 standard.  Note:  I'm referring 
to the 1998 one.  I don't have the revised standard.

 >NO! You _broke_ it! Now you return a pointer to constant, and thats _NOT_ 
what I wanted.

g++ -DCONST= foo.cpp

Then neither the pointer returned, nor the up-cast function are committing 
to const protection.

 >The question was more about cast from "this", like this:

Do this then:

virtual CProvider* getProvider() const { return const_cast<CProvider*>(this); }

Note:  on my team, this code would not be acceptable in a code 
review.  Very dangerous.  If you REALLY have to have a non-const futzing, 
consider putting the functionality IN the class itself, so the class can 
manipulate its own internal state in a "non-const" fashion via a const 
method, yet retain logically const state.  Otherwise, it's an egregious 
violation of the const contract.

 >I do not agree as thats not portable across all compilers.

It is portable across all C++ compilers that I use, and I use quite a few 
different ones on a wide variety of platforms.  It may not be portable 
across some very old compilers.

It's also portable across all C++ compliant compilers.  But that's not 
saying much, because I don't think there are any fully ISO 14882 compliant 
compilers available.  (Hmmm, maybe EDG is fully compliant, I'm not sure.)  :-)

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inherited member( void )const - const to be or not to be
  2004-09-07 12:58 ` Eljay Love-Jensen
@ 2004-09-07 16:35   ` SVisor
  2004-09-07 17:10     ` Eljay Love-Jensen
  0 siblings, 1 reply; 7+ messages in thread
From: SVisor @ 2004-09-07 16:35 UTC (permalink / raw)
  To: gcc-help


Hi and thanks for the reply, but I do not agree...

...
> Don't use _CONST ... that's a reserved symbol.

Well that was news. Anyway it was just "dummy" code.

...
> All symbols starting with underscore followed by a capital letter are 
> reserved.  All symbols with two underscores in a row anywhere are reserved.

name__more is not reserved. __name is reserved. Usage of _Name is not 
recomended, but what I know not reserved. At least thats what I have 
been taught. No one has _ever_ shown proof of anything else (heck no one 
has shown me proof of what Ive learned, I just have accepted it ;-). Do 
you have any link to a list of which combinations are reserved and which 
are not?

> I've fixed your code, works now just fine.  Compiled either way.

NO! You _broke_ it! Now you return a pointer to constant, and thats 
_NOT_ what I wanted. I wanted to hint the compiler that a function does 
not change members, but I can still change members (and call non 
constant member-functions) using the returned pointer (and its legal 
C++). The question was more about cast from "this", like this:

virtual CProvider* getProvider( void )const{ return (CProvider*)this; }

It will compile and provide sane working code, but its C (not C++) 
styled. And the cast is not _safe_: The code would compile even if the 
class would not inherit from CProvider.

...
> BTW:  using the -DCONST=const is preferred.  So much so, that you should 

I do not agree as thats not portable across all compilers. While my 
construct is (well 99.9% of them at least). The code was just dummy code 
to show a point, and make it easy to test. You usually know if something 
is const or not :-).

// Jarmo

-- 

> just have const in the code.  Const correctness should be worked in from 
> the beginning.
> 
> HTH,
> --Eljay
> 
> - - - - - - - - -
> // Compile #1:  g++ -DCONST=const foo.cpp
> // Compile #2:  g++ -DCONST= foo.cpp
> #include <cstdio>
> 
> class CProvider
> {
> protected:
>     const char* str;
> public:
>     CProvider(const char* s)
>     : str(s)
>     {
>         printf("CProvider(%s)\n",str);
>     }
> 
>     void echo() CONST
>     {
>         printf("CProvider(%s)::echo()\n", str);
>     }
> };
> 
> 
> class CAbstract
> {
> public:
>     CAbstract()
>     {
>         printf("CAbstract\n");
>     }
> 
>     virtual ~CAbstract()
>     { }
> 
>     virtual CProvider CONST* getProvider() CONST = 0;
> };
> 
> 
> class CAllocated : public CAbstract
> {
> protected:
>     CProvider* ptr;
> 
> public:
>     CAllocated()
>     {
>         printf("CAllocated\n");
>         ptr = new CProvider("Allocated");
>     }
> 
>     virtual CProvider CONST* getProvider() CONST
>     {
>         return ptr;
>     }
> };
> 
> 
> class CInherited : public CAbstract, public CProvider
> {
> public:
>     CInherited()
>     : CProvider("Inherited")
>     {
>         printf("CInherited\n");
>     }
> 
>     virtual CProvider CONST* getProvider() CONST
>     {
>         return this;
>     }
> };
> 
> 
> int main()
> {
>     CAllocated tmp1;
>     CInherited tmp2;
> 
>     tmp1.getProvider()->echo();
>     tmp2.getProvider()->echo();
> 
>     CAbstract* pTmp1 = &tmp1;
>     CAbstract* pTmp2 = &tmp2;
>     pTmp1->getProvider()->echo();
>     pTmp2->getProvider()->echo();
> }
> 
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Inherited member( void )const - const to be or not to be
  2004-09-06 11:01 SVisor
@ 2004-09-07 12:58 ` Eljay Love-Jensen
  2004-09-07 16:35   ` SVisor
  2004-09-21 13:34 ` Claudio Bley
  1 sibling, 1 reply; 7+ messages in thread
From: Eljay Love-Jensen @ 2004-09-07 12:58 UTC (permalink / raw)
  To: svisor, gcc-help

Hi Jarmo,

Don't use _CONST ... that's a reserved symbol.

All symbols starting with underscore followed by a capital letter are 
reserved.  All symbols with two underscores in a row anywhere are reserved.

I've fixed your code, works now just fine.  Compiled either way.

BTW:  using the -DCONST=const is preferred.  So much so, that you should 
just have const in the code.  Const correctness should be worked in from 
the beginning.

HTH,
--Eljay

- - - - - - - - -
// Compile #1:  g++ -DCONST=const foo.cpp
// Compile #2:  g++ -DCONST= foo.cpp
#include <cstdio>

class CProvider
{
protected:
     const char* str;
public:
     CProvider(const char* s)
     : str(s)
     {
         printf("CProvider(%s)\n",str);
     }

     void echo() CONST
     {
         printf("CProvider(%s)::echo()\n", str);
     }
};


class CAbstract
{
public:
     CAbstract()
     {
         printf("CAbstract\n");
     }

     virtual ~CAbstract()
     { }

     virtual CProvider CONST* getProvider() CONST = 0;
};


class CAllocated : public CAbstract
{
protected:
     CProvider* ptr;

public:
     CAllocated()
     {
         printf("CAllocated\n");
         ptr = new CProvider("Allocated");
     }

     virtual CProvider CONST* getProvider() CONST
     {
         return ptr;
     }
};


class CInherited : public CAbstract, public CProvider
{
public:
     CInherited()
     : CProvider("Inherited")
     {
         printf("CInherited\n");
     }

     virtual CProvider CONST* getProvider() CONST
     {
         return this;
     }
};


int main()
{
     CAllocated tmp1;
     CInherited tmp2;

     tmp1.getProvider()->echo();
     tmp2.getProvider()->echo();

     CAbstract* pTmp1 = &tmp1;
     CAbstract* pTmp2 = &tmp2;
     pTmp1->getProvider()->echo();
     pTmp2->getProvider()->echo();
}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Inherited member( void )const - const to be or not to be
@ 2004-09-06 11:01 SVisor
  2004-09-07 12:58 ` Eljay Love-Jensen
  2004-09-21 13:34 ` Claudio Bley
  0 siblings, 2 replies; 7+ messages in thread
From: SVisor @ 2004-09-06 11:01 UTC (permalink / raw)
  To: gcc-help

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?

// Jarmo

Following should compile with g++ from command line.
To break it use "-D_CONST":


#include <stdio.h>

// #define _CONST // If defined this will not compile
#ifdef _CONST
#define CONST const
#else
#define CONST
#endif

class CProvider
{
protected:
	const char* str;
public:
	CProvider( const char* s ){ str=s; printf( "CProvider(%s)\n",str );}
	void echo( void ){ printf( "CProvider(%s)::echo( )\n",str ); }
};

class CAbstract
{
public:
	CAbstract( void ){ printf( "CAbstract\n" ); }
	virtual CProvider* getProvider( void )CONST=0;
};

class CAllocated : public CAbstract
{
protected:
	CProvider *ptr;

public:
	CAllocated( void ){ printf( "CAllocated\n" ); ptr=new CProvider( 
"Allocated" ); }

	virtual CProvider* getProvider( void )CONST{ return ptr; }
};

class CInherited : public CAbstract,public CProvider
{
public:
	CInherited( void ):CProvider( "Inherited" ){ printf( "CInherited\n" );  }

	virtual CProvider* getProvider( void )CONST{ return this; }
};

int main( void )
{
	CAllocated tmp1;
	CInherited tmp2;

	tmp1.getProvider( )->echo( );
	tmp2.getProvider( )->echo( );
	
	CAbstract* pTmp1=&tmp1;
	CAbstract* pTmp2=&tmp2;
	pTmp1->getProvider( )->echo( );
	pTmp2->getProvider( )->echo( );
	return 0;
}

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-09-21 13:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-07 16:40 Inherited member( void )const - const to be or not to be lrtaylor
  -- strict thread matches above, loose matches on Subject: below --
2004-09-06 11:01 SVisor
2004-09-07 12:58 ` Eljay Love-Jensen
2004-09-07 16:35   ` SVisor
2004-09-07 17:10     ` Eljay Love-Jensen
2004-09-21 13:34 ` Claudio Bley
2004-09-21 13:41   ` Claudio Bley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).