[comp.lang.c++] Can you prohibit member functions from modifying *this?

scott@Apple.COM (scott douglass) (09/16/88)

If I have a class A and I have a const A* ap, should I be allowed to
invoke ap->f() which may modify *ap?

class A {
  private:
        int val;
  public:
        A() { val = 0; }
        void stab(int i) { val = i; }
	int get() { return val; }
};
 
main()
{
        A a;
        a.stab(4);              // this is fine

        const A* ap = &a;
        ap->stab(5);            // this is questionable

	const A ca;
	ca.stab(6);		// this is questionable, too
}

CFront allows all of these, effectively ignoring the const in the cases I
marked as questionable.  It would be useful to prohibit the questionable
cases above but instead be able to delcare a member function (such as get()
above) that had its implicit this argument declared as a const pointer.  I will
even be so bold as to propose an ugly, flawed syntax which I trust someone
can improve on (please):

class A {
  private:
        int val;
  public:
        A() { val = 0; }
        void stab(int i) { val = i; }
  const:
	int get() { return val; }
};
 
main()
{
        A a;
        a.stab(4);              // this is fine
	int i = a.get();	// this is fine, too

        const A* ap = &a;
        ap->stab(5);            // this is illegal
	i = ap->get();		// this is fine, too

	const A ca;
	ca.stab(6);		// this is illegal, too
	i = ca.get();		// this is fine, too
}
				--scott douglass
Any opinions above may be mine and are not necessarily those of Apple Computer.
domain: scott@apple.com  UUCP: {nsc, sun, voder, well, dual}!apple!scott
CSNet: scott@Apple.CSNet  AppleLink: Douglass1