Symbian3/SDK/Source/GUID-27C933F7-4634-57C8-B3D2-45C5563FECA4.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Thu, 21 Jan 2010 18:18:20 +0000
changeset 0 89d6a7a84779
permissions -rw-r--r--
Initial contribution of Documentation_content according to Feature bug 1266 bug 1268 bug 1269 bug 1270 bug 1372 bug 1374 bug 1375 bug 1379 bug 1380 bug 1381 bug 1382 bug 1383 bug 1385

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-27C933F7-4634-57C8-B3D2-45C5563FECA4" xml:lang="en"><title>How
to use dynamic arrays</title><shortdesc>This document describes how to create templated arrays; that is,
an array of a particular type of object.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>All array classes are templated; the template value defines the type of
object which is to form an element of the array. This is true for arrays of
same length and variable length elements. The following example code constructs
an array of same length elements of type <codeph>TMyClass</codeph> using the
flat array buffer <codeph>CArrayFixFlat&lt;class T&gt;</codeph>. The array has
granularity 16:</p>
<codeblock id="GUID-E59CB1C7-4BFD-574B-97DD-28B82BEDB9BB" xml:space="preserve">CArrayFixFlat&lt;TMyClass&gt; array;
...
array = new (ELeave) CArrayFixFlat&lt;TInt&gt;(16);
...</codeblock>
<p>To construct an array of <codeph>TInt</codeph> values with a <i>granularity</i> of
16, implement code as follows:</p>
<codeblock id="GUID-6B599A36-21EE-50B0-BF18-00C84AEC50A7" xml:space="preserve">CArrayFixFlat&lt;TInt&gt;* array;
...
array = new (ELeave) CArrayFixFlat&lt;TInt&gt;(16);</codeblock>
<p>After construction, the array contains no elements and no memory is allocated
to the array buffer.</p>
<p><codeph>CArrayPtrFlat</codeph> is a specialised array where the elements
are <i>pointers</i> to <codeph>CBase</codeph> derived objects. To construct
an array whose elements are pointers to <codeph>CMyClass</codeph> objects:</p>
<codeblock id="GUID-5F63CDA5-2386-562F-A66D-6963A126314D" xml:space="preserve">CArrayPtrFlat&lt;CMyClass&gt;* array;
...
array = new (ELeave) CArrayPtrFlat&lt;CMyClass&gt;(16);</codeblock>
<p>To construct an array of same length elements of type <codeph>TMyClass</codeph> using <codeph>RArray&lt;class
T&gt;</codeph>:</p>
<codeblock id="GUID-13D94D48-B752-5A31-9CC6-BD94DC2DEE3A" xml:space="preserve">RArray&lt;TMyClass&gt; array; // this is on the stack
...
...                     // add entries etc
...
array.Close();          // called before the array goes out of scope</codeblock>
<p>To construct an array of pointers to <codeph>CMyClass</codeph> objects
using <codeph>RPointerArray&lt;class T&gt;</codeph>:</p>
<codeblock id="GUID-151C2968-19F5-5AA0-BB93-DCBFF023B0DA" xml:space="preserve">RPointerArray&lt;CMyClass&gt; array;  // this is on the stack
...
CMyClass* ptr = new (ELeave) CMyClass;
...
array.Append(ptr);
...
array.ResetAndDestroy();       // called before the array goes
                               // out of scope</codeblock>
</conbody></concept>