Computer Science Canada

Value of const/final members in classes?

Author:  wtd [ Sun Jul 02, 2006 3:33 pm ]
Post subject:  Value of const/final members in classes?

So... what's your opinion? Do constant or final (in the case of Java and some others) members (or "instance variables") have merit?

Or should this be purely enforced by the methods a classes exposes?

Author:  [Gandalf] [ Sun Jul 02, 2006 11:03 pm ]
Post subject: 

They're useful for internal use inside a class, no? For constants which you must access externally, I'd use methods.

Author:  wtd [ Mon Jul 03, 2006 4:16 pm ]
Post subject: 

I was actually thinking of just the opposite.

Consider:

code:
class Foo
{
   private:
      int _bar;
 
   public:
      Foo(int b) : _bar(b) { }

      int bar() const
      {
         return _bar;
      }
};


vs.

code:
struct Foo
{
   const int bar;

   Foo(int b) : bar(b) { }
};


: