220 14654 <a15820e4-3c90-400d-bf49-434467d728f6@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: r.s.salkin@gmail.com
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: "const public:" and "const private:" to avoid to
 write "T Get() const" member functions
Date: Thu, 20 Nov 2014 12:22:04 -0800 (PST)
Lines: 190
Approved: news@gmane.org
Message-ID: <a15820e4-3c90-400d-bf49-434467d728f6@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_7200_525277009.1416514924260"
X-Trace: ger.gmane.org 1416514936 3714 80.91.229.3 (20 Nov 2014 20:22:16 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Thu, 20 Nov 2014 20:22:16 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBC47DW5KYQGBB3M2XGRQKGQEPM3P3DA@isocpp.org Thu Nov 20 21:22:09 2014
Return-path: <std-proposals+bncBC47DW5KYQGBB3M2XGRQKGQEPM3P3DA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ig0-f197.google.com ([209.85.213.197])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBC47DW5KYQGBB3M2XGRQKGQEPM3P3DA@isocpp.org>)
	id 1XrYEx-00077B-28
	for gclcip-std-proposals@m.gmane.org; Thu, 20 Nov 2014 21:22:07 +0100
Original-Received: by mail-ig0-f197.google.com with SMTP id r2sf16412291igi.0
        for <gclcip-std-proposals@m.gmane.org>; Thu, 20 Nov 2014 12:22:06 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:message-id:subject:mime-version:content-type
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=vodz3O/XAuYtCkHMBmRi3he+xXVAzCBaZX/NBpfzyaM=;
        b=QeQEPHHZfKPGgW9JbbMP0MXIt9WC/tlBw5HOTthL1JL0PPczpO55qmtLnNrsB9LvTC
         1tVuzMKW+BOMhUag6/ceJlw+gwHXnakZAlbiFaSSbtLkJkVtJsVk8w+awnE4eXdex3yQ
         wmST/Op/mLLwNrlza0HeF9RqOcJq6f46bnrydGnVzYQeGndJH086R5Fkkti+TLTDJu4k
         Em4sphZzjlziTR7nLJQ3ZqHS/829K+9SSu/7KSZWKW43KnmHo1DNdhu8ACNBswOEsYNe
         qvImEA3s9Gi0hNJClvbhcWBSV1r4OahrJduFBxEz68E4Kyv9ianWKt689x4otQg7nAub
         Eubg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:message-id:subject:mime-version
         :content-type:x-original-sender:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=vodz3O/XAuYtCkHMBmRi3he+xXVAzCBaZX/NBpfzyaM=;
        b=gNmnNdP+jjyXVHRYtwX5r/6iokXkMhwEn8MUJW+/lfHST07eGdqz62EmrP14ZhScag
         lwXprhS0H9ZYzURj03T+r8/KxMvEPp/9EQy2yZfR8Pp6wPUAu1siDkhjFPo6uE42oNm8
         ct57/21YWcQ+/RyrAaWyzfOaQrY+USI34YfdbKI1qD1p3n7NcJGOKjqBviwHrszIhk3t
         GAYTp51xfBa/mtw4KomV7u8544DGt08uxti8YwQkIjsIAE2yM7VJQ1O9RJ+mOuem4q88
         CRp2739g/w1CCsXPZn7jK2IPWKWHCaync/9lbqL0ZNiSUiVU3KIWPRdsz4zjybW8Fu/1
         9r6A==
X-Gm-Message-State: ALoCoQmrOFeASLD9H2XEqQYpuVdiPhr05FTYn7Va8X29Q14uM9R9aJtSR0ILp5a2E7iuajNOmDWL
X-Received: by 10.182.33.134 with SMTP id r6mr341772obi.34.1416514926123;
        Thu, 20 Nov 2014 12:22:06 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.97.200 with SMTP id m66ls489002qge.97.gmail; Thu, 20 Nov
 2014 12:22:05 -0800 (PST)
X-Received: by 10.140.102.117 with SMTP id v108mr4980qge.36.1416514925488;
        Thu, 20 Nov 2014 12:22:05 -0800 (PST)
X-Original-Sender: r.s.salkin@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:14654
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/14654>

------=_Part_7200_525277009.1416514924260
Content-Type: multipart/alternative; 
	boundary="----=_Part_7201_25417446.1416514924260"

------=_Part_7201_25417446.1416514924260
Content-Type: text/plain; charset=UTF-8

In order to protect member variables from improper use, normally 
get-functions are used. If the variable in question is constant, the 
variable can also be declared const and be accessed directly by putting it 
into the "public:"-section.
The disadvantage of the first approch is rather lengthy code for a simple 
task: read-only access outside the class, read/write-access inside. The 
second approach is not as general as the first since it can be only used 
only for constant variables.

Why not introducing a keyword showing that a member variable can be altered 
within the class but not outside, still having read-access from outside?

Proposal 1)  
Add "const public:" and "const protected:" to the existing "public:", 
"protected:", and "private:" keywords. Examples below. It is unclear what 
happens with functions after those new keywords. They could be forbidden 
(compiler error).

Examples:

Instead of:

class A
{
public:
    int GetX() const
    {
        return m_iX;
    };
private:
    int m_iX;
};

write

class A
{
const public:
 
    int m_iX;
};

This means m_iX is visible and readable from outside the class, but not 
writable. From within it is writable. 

Instead of

class B
{
public:
    int GetX() const
    {
        return m_iX;
    };
protected:
    int m_iX;
};

write

class B
{
const protected:
 
    int m_iX;
};

Here m_iX cannot be read outside the class, but it can be read by a derived 
class (but not written). Within A it can be read or written.

class C : public B
{
    void Function()
    {
        int iY=m_iX*3;  // allowed
        m_iX=4;           // forbidden
    }
};


Proposal 2) 
Use a keyword like "readonly" preceding the member variable type.
Example:

class A
{
public:
    const int m_iW;               // readonly access inside and outside 
class
    readonly int m_iX;            // readonly access outside, 
read/write-access inside class
    int m_iY;                         // read/write access inside and 
outside (as usual)

protected:
   readonly int m_iZ;             // read but not write access from derived 
class

private:
   readonly int m_iL;            // not really meaningful, should be not 
allowed
};

-- 

--- 
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_7201_25417446.1416514924260
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">In order to protect member variables from improper use, no=
rmally get-functions are used. If the variable in question is constant, the=
 variable can also be declared const and be accessed directly by putting it=
 into the "public:"-section.<br>The disadvantage of the first approch is ra=
ther lengthy code for a simple task: read-only access outside the class, re=
ad/write-access inside. The second approach is not as general as the first =
since it can be only used only for constant variables.<br><br>Why not intro=
ducing a keyword showing that a member variable can be altered within the c=
lass but not outside, still having read-access from outside?<br><br>Proposa=
l 1)&nbsp; <br>Add "const public:" and "const protected:" to the existing "=
public:", "protected:", and "private:" keywords. Examples below. It is uncl=
ear what happens with functions after those new keywords. They could be for=
bidden (compiler error).<br><br>Examples:<br><br>Instead of:<br><span style=
=3D"color: rgb(0, 0, 255);"><br>class A<br>{<br>public:<br>&nbsp;&nbsp;&nbs=
p; int GetX() const<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp; return m_iX;<br>&nbsp;&nbsp;&nbsp; };<br>private:<br>&nbsp;&=
nbsp;&nbsp; int m_iX;<br>};</span><br><br>write<br><br><span style=3D"color=
: rgb(0, 0, 255);">class A<br>{<br>const public:<br>&nbsp;<br>&nbsp;&nbsp;&=
nbsp; int m_iX;<br>};<br></span><br>This means m_iX is visible and readable=
 from outside the class, but not writable. From within it is writable. <br>=
<br>Instead of<br><br><span style=3D"color: rgb(0, 0, 255);">class B<br>{<b=
r>public:<br>&nbsp;&nbsp;&nbsp; int GetX() const<br>&nbsp;&nbsp;&nbsp; {<br=
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return m_iX;<br>&nbsp;&nbsp;&nb=
sp; };<br>protected:<br>&nbsp;&nbsp;&nbsp; int m_iX;<br>};</span><br><br>wr=
ite<br><br><span style=3D"color: rgb(0, 0, 255);">class B<br>{<br>const pro=
tected:<br>&nbsp;<br>&nbsp;&nbsp;&nbsp; int m_iX;<br>};</span><br><br>Here =
m_iX cannot be read outside the class, but it can be read by a derived clas=
s (but not written). Within A it can be read or written.<br><br><span style=
=3D"color: rgb(0, 0, 255);">class C : public B<br>{<br>&nbsp;&nbsp;&nbsp; v=
oid Function()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp; int iY=3Dm_iX*3;&nbsp; // allowed<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp; m_iX=3D4;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp; // forbidden<br>&nbsp;&nbsp;&nbsp; }<br>};</span><br><br><br>Prop=
osal 2) <br>Use a keyword like "readonly" preceding the member variable typ=
e.<br>Example:<br><br><span style=3D"color: rgb(0, 0, 255);">class A<br>{<b=
r>public:<br>&nbsp;&nbsp;&nbsp; const int m_iW;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // readonly access=
 inside and outside class<br>&nbsp;&nbsp;&nbsp; readonly int m_iX;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // readonly acces=
s outside, read/write-access inside class<br>&nbsp;&nbsp;&nbsp; int m_iY;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // read=
/write access inside and outside (as usual)<br><br>protected:<br>&nbsp;&nbs=
p; readonly int m_iZ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp; // read but not write access from derived class<br><br>p=
rivate:<br>&nbsp;&nbsp; readonly int m_iL;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // not really meaningful, should be not a=
llowed<br>};</span><br></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_7201_25417446.1416514924260--
------=_Part_7200_525277009.1416514924260--

.
