Path: utzoo!attcan!uunet!pyrdc!pyrnj!rutgers!apple!scott From: scott@Apple.COM (scott douglass) Newsgroups: comp.lang.c++ Subject: Can you prohibit member functions from modifying *this? Message-ID: <17192@apple.Apple.COM> Date: 15 Sep 88 19:55:54 GMT Reply-To: scott@apple.com (scott douglass) Organization: Apple Computer, Inc. Cupertion, CA Lines: 60 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