Topic: readonly member variables ?


Author: jones@cais.cais.com (Ben Jones)
Date: 23 Jun 1994 15:09:34 GMT
Raw View
Yung Tin Ho (cs270196@hkpu01.hkp.hk) wrote:
: David Lohmann (dlohmann@uhunix.uhcc.Hawaii.Edu) wrote:
: : I am soliciting opinions for an
: : alternative, possible more readable and efficient way of
: : achieving the same effect.  With another access specifier, let's
: : say ``readonly'', programmers could let non-member fns access
: : member variables as if they were constants.  There would be
: : no need for special access fns.  Consider the following code:

: : class Employee {
: : public:
: :  Employee();
: :  ~Employee();

: : readonly:
: :  char* name; // same as const char* const name
: :    // to non-members
: :  long  id; // same as const long id

: : private:
: :  char* really_private_info;  // no outside access
: : };

: : Has any ANSI/ISO C++ commitee considered something similar?
: : Does anyone care to comment?

: :  Peter

: The reason for introducing protected and private is no only preventing
: other from modifying the object's context but also isolating the
: implementation from interface.  If we let all non-member functions
: have access to "readonly" member "char *name", we may have problem when
: someday someone change it to "String name".

It seems to me that "readonly" means simply "public" but not-writable by
non-members.  If you really want non-members to go through a function to
get to a member because you really do want to hide the nature of the
member, that is another matter entirely.

The one bad thing about going through a function is that it looks clumsy.
It would be nice if parameter-less functions could be called without ()
like in Ada and Pascal so that you could hide the nature of a member when
you want to.  Since a function name by itself means "pointer to function"
in C/C++, we could make use of a qualifier:

    class X
    {
        ...
        char *m() auto;
    } x1;

The "auto" would indicate that when "m" occurred in any expression, it would
be automatically executed:

    printf("%d\n",x1.m);

Both "readonly" and automatic functions are implemented in the ARC++
extension to the C/C++ language.

Ben Jones
jones@arsoftware.arclch.com









Author: cs270196@hkpu01.hkp.hk (Yung Tin Ho)
Date: Tue, 21 Jun 1994 13:47:14 GMT
Raw View
David Lohmann (dlohmann@uhunix.uhcc.Hawaii.Edu) wrote:
: I seems that when we design an application with C++, we follow
: the same conventions... limiting access to member variables with
: private and protected access specifiers, then providing access
: through controlled, inlined, efficient member fns.

: This is reasonable, however, I am soliciting opinions for an
: alternative, possible more readable and efficient way of
: achieving the same effect.  With another access specifier, let's
: say ``readonly'', programmers could let non-member fns access
: member variables as if they were constants.  There would be
: no need for special access fns.  Consider the following code:

: class Employee {
: public:
:  Employee();
:  ~Employee();

: readonly:
:  char* name; // same as const char* const name
:    // to non-members
:  long  id; // same as const long id

: private:
:  char* really_private_info;  // no outside access
: };

: Has any ANSI/ISO C++ commitee considered something similar?
: Does anyone care to comment?

:  Peter

The reason for introducing protected and private is no only preventing
other from modifying the object's context but also isolating the
implementation from interface.  If we let all non-member functions
have access to "readonly" member "char *name", we may have problem when
someday someone change it to "String name".

Coincidently, stroustrup had thought about to use "readonly" as the
keyword of today's "const" before (but not as your suggested meaning).
You may refer to this interesting history on section 3.8 of "the design
and evolution of c++" by stroustrup.

regards.
- thyung














Author: dlohmann@uhunix.uhcc.Hawaii.Edu (David Lohmann)
Date: Fri, 17 Jun 1994 20:17:26 GMT
Raw View
I seems that when we design an application with C++, we follow
the same conventions... limiting access to member variables with
private and protected access specifiers, then providing access
through controlled, inlined, efficient member fns.

This is reasonable, however, I am soliciting opinions for an
alternative, possible more readable and efficient way of
achieving the same effect.  With another access specifier, let's
say ``readonly'', programmers could let non-member fns access
member variables as if they were constants.  There would be
no need for special access fns.  Consider the following code:

class Employee {
public:
 Employee();
 ~Employee();

readonly:
 char* name; // same as const char* const name
   // to non-members
 long  id; // same as const long id

private:
 char* really_private_info;  // no outside access
};

Has any ANSI/ISO C++ commitee considered something similar?
Does anyone care to comment?

 Peter

--
//-----------------------------------------------------------
// D. Peter Lohmann
//-----------------------------------------------------------