Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!europa.asd.contel.com!gatech!bloom-beacon!eru!hagbard!sunic!chalmers.se!appli!niklas From: niklas@appli.se (Niklas Hallqvist) Newsgroups: comp.lang.c++ Subject: Re: MEMBER ACCESS Message-ID: <1390@appli.se> Date: 3 Jun 91 10:17:16 GMT References: <97edfa6b92f028446f60@rose.uucp> Organization: Applitron Datasystem AB, GOTHENBURG, SWEDEN Lines: 51 phil.calvin@rose.uucp (PHIL CALVIN) writes: > A language feature I'd like to see... There is language support for this already... > A class has certain data members, for efficiency reasons, I would like >to grant the USER read-only access. > EX: > class COORD > { > public: > // Don't modify just read.. > int x,y; > ... > }; > Obviously, the comment does little more than _encourage_ the user to >modify the value. My proposal is to have language support for this >concept: class COORD { int _x; int _y; public: const int& x; const int& y; COORD() : x(_x), y(_y) {} }; int main(int, char**) { COORD p; int X = p.x; int Y = p.y; // p.x = X; // ERROR! // p.y = Y; // ERROR! } As you can see, no new language constructs are needed. Niklas -- Niklas Hallqvist Phone: +46-(0)31-40 75 00 Applitron Datasystem Fax: +46-(0)31-83 39 50 Molndalsvagen 95 Email: niklas@appli.se S-412 63 GOTEBORG, Sweden mcsun!sunic!chalmers!appli!niklas