Inherited member( void )const - const to be or not to be

Eljay Love-Jensen eljay@adobe.com
Tue Sep 7 12:58:00 GMT 2004


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();
}



More information about the Gcc-help mailing list