delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/03/17/14:26:47

From: "Campbell, Rolf [SKY:1U32:EXCH]" <moscoop AT americasm01 DOT nt DOT com>
Newsgroups: comp.os.msdos.djgpp,comp.lang.c++
Subject: Accessor methods are annoying. (Was: Re: TABLE OF POINTERS TO FUNCTIONS
+ OTHER)
Date: Fri, 17 Mar 2000 12:35:56 -0500
Organization: Nortel Networks
Lines: 33
Message-ID: <38D26CFC.16E69D47@americasm01.nt.com>
References: <8am435$j8i$1 AT tron DOT sci DOT fi> <38D1079A DOT 45700792 AT americasm01 DOT nt DOT com> <l4t2dsg398i4e02b147mv18i89a5tvo0nq AT 4ax DOT com>
NNTP-Posting-Host: wmerh0tk.ca.nortel.com
Mime-Version: 1.0
X-Mailer: Mozilla 4.72C-CCK-MCD [en] (X11; I; HP-UX B.10.20 9000/785)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Damian Yerrick wrote:

> >And most of the time they are just used to force 'read-only' for
> >public access.  To start with, I think this should have been part
> >of the C++ specification (const public: could mean that outside
> >that class's scope it is treated as const).
>
> But that would break all existing programs if you figured out a better
> implementation of the class that didn't use that variable.

    That is true for my example about 'const public:'.  But many other
languages offer built-in method of trapping reads/writes to variables.
Using this scheme, accessors and mutators only need to be written when
the library changes instead of all the time.  In C++ this ability is not
'built-in', but it is available (I've written a templatized library to do
just this).  That class acts just like any other class with a few
exceptions.  I think it is impossible to get it perfect in C++ (I can't
cope with passing that member variable to a function by reference).  But
just about everything else works...

>  Data
> hiding is one of the reasons why accessor methods exist.  How hard is
>    my_string.length()
> when string::length() is inlined to return my_string._length?

    Data hiding is good for backwards compatability, but you don't need
backwards compatability until the class changes...

--
(\/) Rolf Campbell (\/)



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019