220 4053 <27175469-a158-4bc1-9f30-66e3a647e931@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Hariharan Subramanian <tohari@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: A cleaner way to define nested class/structure
Date: Mon, 29 Apr 2013 00:07:16 -0700 (PDT)
Lines: 120
Approved: news@gmane.org
Message-ID: <27175469-a158-4bc1-9f30-66e3a647e931@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_28_17440801.1367219236949"
X-Trace: ger.gmane.org 1367219239 19549 80.91.229.3 (29 Apr 2013 07:07:19 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Mon, 29 Apr 2013 07:07:19 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCJZFFHTQQIBBKFY7CFQKGQEVDPV47Y@isocpp.org Mon Apr 29 09:07:23 2013
Return-path: <std-proposals+bncBCJZFFHTQQIBBKFY7CFQKGQEVDPV47Y@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-gh0-f200.google.com ([209.85.160.200])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCJZFFHTQQIBBKFY7CFQKGQEVDPV47Y@isocpp.org>)
	id 1UWiBF-0004FC-JN
	for gclcip-std-proposals@m.gmane.org; Mon, 29 Apr 2013 09:07:22 +0200
Original-Received: by mail-gh0-f200.google.com with SMTP id 10sf9507596ghy.7
        for <gclcip-std-proposals@m.gmane.org>; Mon, 29 Apr 2013 00:07:20 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=x-received:x-beenthere:x-received:date:from:to:message-id:subject
         :mime-version:x-original-sender:reply-to:precedence:mailing-list
         :list-id:x-google-group-id:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe:content-type;
        bh=zObKBXKejLCro/fYHo3LtmK9h+/z63TZI2XTbUQMIBM=;
        b=PUgOU2MMZq7NWvv6OR04zbK1MeRqLqux9pz784jivf/2W+4CA/6AirXs9G8NNrysI2
         EfTu/CnLnN069IItpR0EE50k0cdoJGfZg9I6d4cXpJ/HJnkgSvpmPGsQWRtb+j5BQg/R
         dfirjkoAgvl8C/cY5xh+yIBtAwXGHxy+EmQRBhgBwwd42V7KhOez9MNWhGlDlkCo0ufc
         ePh2fLq6Z94BNVyJUAnf8odBBG0HVJHU2c+fuOLfjcpsSa77AxmE81bb/ZgPwI6dY6p5
         PSBjt4avLNbcuPYhdKZZd4mK+SbhHXsrH+TxY+L4wJ4bOCKtMscB63xshqCxFW37Flx5
         lydg==
X-Received: by 10.236.137.14 with SMTP id x14mr23099336yhi.4.1367219240697;
        Mon, 29 Apr 2013 00:07:20 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.49.94.44 with SMTP id cz12ls2724943qeb.67.gmail; Mon, 29 Apr
 2013 00:07:17 -0700 (PDT)
X-Received: by 10.49.95.231 with SMTP id dn7mr4289584qeb.39.1367219237502;
        Mon, 29 Apr 2013 00:07:17 -0700 (PDT)
X-Original-Sender: tohari@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?hl=en>,
 <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?hl=en&topic=25838>,
 <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe?hl=en>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe?hl=en>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:4053
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/4053>

------=_Part_28_17440801.1367219236949
Content-Type: text/plain; charset=ISO-8859-1

Problem:

class Outer {
class Inner1;
class Inner2;

Inner1 inner1;
Inner2 *inner2;
};

class Outer::Inner1 {
int i1;
};

class Outer::Inner2 {
int i2;
};

The code above results in compilation error because the class Inner1 is not 
defined before the declaration of the object inner1. Inner2 obviously has 
no problem.Defining the nested classes within the Outer class hampers 
readability.

Solution:

class Outer; //Declare the Outer class

class Outer::Inner1 { //Define the Inner1 class
int i1;
};

class Outer {           //If this class is not defined in the same 
compilation unit then it should result in an error because Inner1 is 
defined as a nested class but the nesting class is undefined.
class Inner1;   //If this is absent then it should result in compilation 
error
class Inner2;

Inner1 inner1;
Inner2 *inner2;
};

class Outer::Inner2 {
int i2;
};

This solution honours the C++ convention that the class must be defined 
before creating an object of its type. This certainly improves readability.

Hariharan S

-- 

--- 
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/?hl=en.



------=_Part_28_17440801.1367219236949
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>Problem:</div><div><br></div><div>class Outer {</div><div><span class=
=3D"Apple-tab-span" style=3D"white-space:pre">	</span>class Inner1;</div><d=
iv><span class=3D"Apple-tab-span" style=3D"white-space: pre;">	</span>class=
 Inner2;<br></div><div><br></div><div><span class=3D"Apple-tab-span" style=
=3D"white-space:pre">	</span>Inner1 inner1;</div><div><span class=3D"Apple-=
tab-span" style=3D"white-space: pre;">	</span>Inner2 *inner2;</div><div>};<=
/div><div><br></div><div>class Outer::Inner1 {</div><div><span class=3D"App=
le-tab-span" style=3D"white-space:pre">	</span>int i1;</div><div>};</div><d=
iv><br></div><div><div>class Outer::Inner2 {</div><div><span class=3D"Apple=
-tab-span" style=3D"white-space: pre;">	</span>int i2;</div><div>};</div></=
div><div><br></div><div>The code above results in compilation error because=
 the class Inner1 is not defined before the declaration of the object inner=
1. Inner2 obviously has no problem.Defining the nested classes within the O=
uter class hampers readability.</div><div><br></div><div>Solution:</div><di=
v><br></div><div>class Outer; //Declare the Outer class</div><div><br></div=
><div><div>class Outer::Inner1 { //Define the Inner1 class</div><div><span =
class=3D"Apple-tab-span" style=3D"white-space: pre;">	</span>int i1;</div><=
div>};</div></div><div><br></div><div><div>class Outer { &nbsp; &nbsp; &nbs=
p; &nbsp; &nbsp; //If this class is not defined in the same compilation uni=
t then it should result in an error because Inner1 is defined as a nested c=
lass but the nesting class is undefined.</div><div><span class=3D"Apple-tab=
-span" style=3D"white-space: pre;">	</span>class Inner1; &nbsp; //If this i=
s absent then it should result in compilation error</div><div><span class=
=3D"Apple-tab-span" style=3D"white-space: pre;">	</span>class Inner2;<br></=
div><div><br></div><div><span class=3D"Apple-tab-span" style=3D"white-space=
: pre;">	</span>Inner1 inner1;</div><div><span class=3D"Apple-tab-span" sty=
le=3D"white-space: pre;">	</span>Inner2 *inner2;</div><div>};</div></div><d=
iv><br></div><div><div><div>class Outer::Inner2 {</div><div><span class=3D"=
Apple-tab-span" style=3D"white-space: pre;">	</span>int i2;</div><div>};</d=
iv></div></div><div><br></div><div>This solution honours the C++ convention=
 that the class must be defined before creating an object of its type. This=
 certainly improves readability.</div><div><br></div><div>Hariharan S</div>=
<div><br></div>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_28_17440801.1367219236949--

.
