From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26782 invoked by alias); 7 Sep 2004 16:40:22 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 26756 invoked from network); 7 Sep 2004 16:40:19 -0000 Received: from unknown (HELO masquerade.micron.com) (137.201.242.130) by sourceware.org with SMTP; 7 Sep 2004 16:40:19 -0000 Received: from mail-srv1.micron.com (localhost [127.0.0.1]) by masquerade.micron.com (8.12.9/8.12.2) with ESMTP id i87GeIoN015511 for ; Tue, 7 Sep 2004 10:40:18 -0600 (MDT) Received: from ntxboimbx07.micron.com (ntxboimbx07.micron.com [137.201.80.94]) by mail-srv1.micron.com (8.12.9/8.12.2) with ESMTP id i87GeGuK015506; Tue, 7 Sep 2004 10:40:17 -0600 (MDT) From: lrtaylor@micron.com content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: RE: Inherited member( void )const - const to be or not to be Date: Tue, 07 Sep 2004 16:40:00 -0000 Message-ID: <363801FFD7B74240A329CEC3F7FE4CC40309617B@ntxboimbx07.micron.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: To: , X-Scanned-By: MIMEDefang 2.37 X-SW-Source: 2004-09/txt/msg00053.txt.bz2 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=20 > 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=20 recomended, but what I know not reserved. At least thats what I have=20 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=20 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=20 _NOT_ what I wanted. I wanted to hint the compiler that a function does=20 not change members, but I can still change members (and call non=20 constant member-functions) using the returned pointer (and its legal=20 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++)=20 styled. And the cast is not _safe_: The code would compile even if the=20 class would not inherit from CProvider. ... > BTW: using the -DCONST=3Dconst is preferred. So much so, that you should=20 I do not agree as thats not portable across all compilers. While my=20 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 --=20 > just have const in the code. Const correctness should be worked in from=20 > the beginning. >=20 > HTH, > --Eljay >=20 > - - - - - - - - - > // Compile #1: g++ -DCONST=3Dconst foo.cpp > // Compile #2: g++ -DCONST=3D foo.cpp > #include >=20 > class CProvider > { > protected: > const char* str; > public: > CProvider(const char* s) > : str(s) > { > printf("CProvider(%s)\n",str); > } >=20 > void echo() CONST > { > printf("CProvider(%s)::echo()\n", str); > } > }; >=20 >=20 > class CAbstract > { > public: > CAbstract() > { > printf("CAbstract\n"); > } >=20 > virtual ~CAbstract() > { } >=20 > virtual CProvider CONST* getProvider() CONST =3D 0; > }; >=20 >=20 > class CAllocated : public CAbstract > { > protected: > CProvider* ptr; >=20 > public: > CAllocated() > { > printf("CAllocated\n"); > ptr =3D new CProvider("Allocated"); > } >=20 > virtual CProvider CONST* getProvider() CONST > { > return ptr; > } > }; >=20 >=20 > class CInherited : public CAbstract, public CProvider > { > public: > CInherited() > : CProvider("Inherited") > { > printf("CInherited\n"); > } >=20 > virtual CProvider CONST* getProvider() CONST > { > return this; > } > }; >=20 >=20 > int main() > { > CAllocated tmp1; > CInherited tmp2; >=20 > tmp1.getProvider()->echo(); > tmp2.getProvider()->echo(); >=20 > CAbstract* pTmp1 =3D &tmp1; > CAbstract* pTmp2 =3D &tmp2; > pTmp1->getProvider()->echo(); > pTmp2->getProvider()->echo(); > } >=20 >=20