From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3835 invoked by alias); 6 Sep 2004 11:01:26 -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 3826 invoked from network); 6 Sep 2004 11:01:23 -0000 Received: from unknown (HELO main.gmane.org) (80.91.224.249) by sourceware.org with SMTP; 6 Sep 2004 11:01:23 -0000 Received: from root by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 1C4HFT-00041r-00 for ; Mon, 06 Sep 2004 13:01:23 +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 ; Mon, 06 Sep 2004 13:01:23 +0200 Received: from svisor by h35n1fls34o828.telia.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 06 Sep 2004 13:01:23 +0200 To: gcc-help@gcc.gnu.org From: SVisor Subject: Inherited member( void )const - const to be or not to be Date: Mon, 06 Sep 2004 11:01:00 -0000 Message-ID: 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 X-SW-Source: 2004-09/txt/msg00031.txt.bz2 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 // #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; }