From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24139 invoked by alias); 7 Sep 2004 16:35:12 -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 24061 invoked from network); 7 Sep 2004 16:35:10 -0000 Received: from unknown (HELO main.gmane.org) (80.91.224.249) by sourceware.org with SMTP; 7 Sep 2004 16:35:10 -0000 Received: from list by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 1C4iw1-000849-00 for ; Tue, 07 Sep 2004 18:35:09 +0200 Received: from h35n1fls34o828.telia.com ([213.65.103.35]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 07 Sep 2004 18:35:09 +0200 Received: from svisor by h35n1fls34o828.telia.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 07 Sep 2004 18:35:09 +0200 To: gcc-help@gcc.gnu.org From: SVisor Subject: Re: Inherited member( void )const - const to be or not to be Date: Tue, 07 Sep 2004 16:35:00 -0000 Message-ID: References: <6.1.2.0.2.20040907075501.01eafe30@iplan-mn.corp.adobe.com> Reply-To: svisor@lycos.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: h35n1fls34o828.telia.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8a3) Gecko/20040902 In-Reply-To: <6.1.2.0.2.20040907075501.01eafe30@iplan-mn.corp.adobe.com> X-SW-Source: 2004-09/txt/msg00052.txt.bz2 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 > > 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(); > } > >