<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Create array of objects with different constructor parameters]]></title><description><![CDATA[<p>Hello.</p>
<p>Let us suppose, that I have quaternary tree. Each tree node can have either zero or 4 children. The children are LB,RB,LU and RU nodes. A tree node is the class:</p>
<pre><code class="language-cpp">class Node
{
public:
  enum position{pLB=0,pRB,pLU,pRU,pRoot};

private:
  Node *parent;   //The parent of the node
  position pos;   //Position of the node in the parent
  Node *children; //Pointer to array of 4 children (if any) or NULL

public:
  Node(void): parent(NULL), pos(pRoot), children(NULL)  //Create root node
  { ; }

  Node(Node *parent, position pos): parent(parent), pos(pos), children(NULL) //Create node having parent
  { ; }

  ~Node(void)
  {
    if(children) delete[] children;
  }

  void MakeChildren(void)
  {
    if(children) return;

    children = new Node[4](this,???); //How to create 4 children with (this,LB),(this,RB),(this,LU),(this,RU) constructor parameters?
  }
};
</code></pre>
<p>I do not have 4 pointers to children nodes because:</p>
<ol>
<li>node can have either 0 or 4 children</li>
<li>4 pointers need more memory then 1 pointer</li>
<li>4 memory allocations by 1 node are slower than 1 memory allocation for 4 nodes</li>
</ol>
<p>How to create 4 nodes simultaneously? I need to pass them parent and their positions in the parent.</p>
<p>I am using placement new now, but it is ugly.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/214888/create-array-of-objects-with-different-constructor-parameters</link><generator>RSS for Node</generator><lastBuildDate>Tue, 13 Oct 2026 00:24:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/214888.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 04 Jun 2008 10:02:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Wed, 04 Jun 2008 10:19:09 GMT]]></title><description><![CDATA[<p>Hello.</p>
<p>Let us suppose, that I have quaternary tree. Each tree node can have either zero or 4 children. The children are LB,RB,LU and RU nodes. A tree node is the class:</p>
<pre><code class="language-cpp">class Node
{
public:
  enum position{pLB=0,pRB,pLU,pRU,pRoot};

private:
  Node *parent;   //The parent of the node
  position pos;   //Position of the node in the parent
  Node *children; //Pointer to array of 4 children (if any) or NULL

public:
  Node(void): parent(NULL), pos(pRoot), children(NULL)  //Create root node
  { ; }

  Node(Node *parent, position pos): parent(parent), pos(pos), children(NULL) //Create node having parent
  { ; }

  ~Node(void)
  {
    if(children) delete[] children;
  }

  void MakeChildren(void)
  {
    if(children) return;

    children = new Node[4](this,???); //How to create 4 children with (this,LB),(this,RB),(this,LU),(this,RU) constructor parameters?
  }
};
</code></pre>
<p>I do not have 4 pointers to children nodes because:</p>
<ol>
<li>node can have either 0 or 4 children</li>
<li>4 pointers need more memory then 1 pointer</li>
<li>4 memory allocations by 1 node are slower than 1 memory allocation for 4 nodes</li>
</ol>
<p>How to create 4 nodes simultaneously? I need to pass them parent and their positions in the parent.</p>
<p>I am using placement new now, but it is ugly.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1522359</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522359</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Wed, 04 Jun 2008 10:19:09 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Wed, 04 Jun 2008 10:32:52 GMT]]></title><description><![CDATA[<ol>
<li>That is a quadtree, not an octree.</li>
<li>Why do you want to create them simultaneously? Why not iteratively in a loop? This all together seems to me as an unnecessary microoptimization try.</li>
</ol>
]]></description><link>https://www.c-plusplus.net/forum/post/1522375</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522375</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Wed, 04 Jun 2008 10:32:52 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Wed, 04 Jun 2008 10:43:08 GMT]]></title><description><![CDATA[<p>Constructur arguments cannot be passed directly in an array new expression. A default constructor may take default arguments, but since those are looked up in the scope of the class (which is where they are declared) it would involve objects with static storage duration to be used in your case which may not be desirable, esp. in multi-threaded environments.<br />
It may make more sense to aggregate the child nodes in a separate class which upon construction initialises each distinct member in turn.<br />
placement new isn't necessarily bad, if you're adventurous you could also employ the generate algorithm in combination with raw_storage_iterator.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1522384</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522384</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 04 Jun 2008 10:43:08 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Wed, 04 Jun 2008 10:58:37 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<ol>
<li>That is a quadtree, not an octree.</li>
<li>Why do you want to create them simultaneously? Why not iteratively in a loop? This all together seems to me as an unnecessary microoptimization try.</li>
</ol>
</blockquote>
<ol>
<li>I noticed this. Already corrected.</li>
<li>I cannot make them in a loop because <code>children</code> in not &quot;array of pointers&quot;, it is &quot;pointer to array&quot;.</li>
</ol>
]]></description><link>https://www.c-plusplus.net/forum/post/1522389</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522389</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Wed, 04 Jun 2008 10:58:37 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Wed, 04 Jun 2008 10:58:06 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>It may make more sense to aggregate the child nodes in a separate class which upon construction initialises each distinct member in turn.</p>
</blockquote>
<p>Excellent!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1522391</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522391</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Wed, 04 Jun 2008 10:58:06 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Wed, 04 Jun 2008 12:05:33 GMT]]></title><description><![CDATA[<p>or placement new</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1522434</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522434</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Wed, 04 Jun 2008 12:05:33 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Wed, 04 Jun 2008 13:11:21 GMT]]></title><description><![CDATA[<p>Hm... Strange error:</p>
<pre><code class="language-cpp">enum tChildPos
{ cpxyz=0,cpXyz,cpxYz,xpXYz,cpxyZ,cpXyZ,cpxYZ,xpXYZ,cpRoot };

class CChildren
{
private:
	CCell cells[8];

public:
	CChildren(Cell *parent);

	inline CCell &amp;operator[](tChildPos p){return cells[p];}
};

CChildren::CChildren(Cell *parent):
	cells[cpxyz](parent,cpxyz),cells[cpXyz](parent,cpXyz), //It does not like [] :(((
	cells[cpxYz](parent,cpxYz),cells[cpXYz](parent,cpXYz),
	cells[cpxyZ](parent,cpxyZ),cells[cpXyZ](parent,cpXyZ),
	cells[cpxYZ](parent,cpxYZ),cells[cpXYZ](parent,cpXYZ)
{
	;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1522489</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522489</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Wed, 04 Jun 2008 13:11:21 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Wed, 04 Jun 2008 16:40:03 GMT]]></title><description><![CDATA[<p>There is no syntactic form to initialise an array member in a constructor with specific arguments. That situation is essentially the same as it is for new expressions, the only possible initialiser for arrays is an empty one which results in value-initialization (and so default construction of its elements here). C++0x will bring significant improvements to this. Within the current standard you will need to use individually named members</p>
<pre><code class="language-cpp">class CChildren
{
private:
    CCell cell0,cell1,cell2,cell3,cell4,cell5,cell6,cell7;
...

CChildren::CChildren(Cell *parent)
:   cell0(parent,cpxyz),cell1(parent,cpxyz),
    cell2(parent,cpxyz),cell3(parent,cpxyz),
    cell4(parent,cpxyz),cell5(parent,cpxyz),
    cell6(parent,cpxyz),cell7(parent,cpxyz)
{}
</code></pre>
<p>It can be awkward to define subscripting in this case. A union may help but unfortunately, while consecutive member of a class must be allocated at increasing addresses if no intervening access specifier is present, there may be internal padding other than that which is strictly neccessary to achieve proper alignment (padding is never needed for that purpose between members of the same type). However, because of its usefulness and wide support by compilers it can still make sense to rely on no padding between members and write thus</p>
<pre><code class="language-cpp">class CChildren
{
...
    CCell&amp; operator[](tChildPos p)
    {
        typedef CCell array_type[8];
        // fail, if compiler unexpectedly adds padding:
        // BOOST_STATIC_ASSERT(( sizeof( array_type ) == sizeof( CChildren ) )); 
        return reinterpret_cast&lt;array_type&amp;&gt;(cell0)[p];
    }
...
</code></pre>
<p>on a side note: don't use capital C to signify classes. That letter (similar to capital T in the Borland world) was originally introduced to prevent name clashes with user declared ids, therefore using it for your classes subverts its purpose. Questionable as the Hungarian notation may be, that capital C has nothing to do with it. When not in Rome don't do as Romans do (i.e. when you're not working on code of the frameworks in question).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1522521</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522521</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 04 Jun 2008 16:40:03 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Wed, 04 Jun 2008 17:42:30 GMT]]></title><description><![CDATA[<p>One year ago you said me that I should not use underscores ( <code>_Cell</code> ) to identify classes. Now you say that I should not use letter &quot;C&quot; ( <code>CCell</code> ) for that purpose. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>What is a correct method to distinguish between types names and variables names?<br />
In word processors I can use bold or red text for that purpose. But in C++ I can not use custom formatting <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>It will be good that red and green identifiers with same name are considered as different identifiers.</p>
<p>By the way, Visual Studio alows me to use any unicode symbols for identifiers:<br />
double const π(3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117);</p>
<p>Is that portable?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1522702</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522702</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Wed, 04 Jun 2008 17:42:30 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Wed, 04 Jun 2008 17:49:22 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>One year ago you said me that I should not use underscores ( <code>_Cell</code> ) to identify classes. Now you say that I should not use letter &quot;C&quot; ( <code>CCell</code> ) for that purpose. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>What is a correct method to distinguish between types names and variables names?<br />
In word processors I can use bold or red text for that purpose. But in C++ I can not use custom formatting <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>It will be good that red and green identifiers with same name are considered as different identifiers.</p>
</blockquote>
<p>I do not use prefixes, I prefer diffrent styles of writing:</p>
<pre><code class="language-cpp">class Class;
int my_var(0);
const int MY_CONST(0);
</code></pre>
<p>SAn schrieb:</p>
<blockquote>
<p>By the way, Visual Studio alows me to use any unicode symbols for identifiers:<br />
double const π(3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117);</p>
<p>Is that portable?</p>
</blockquote>
<p>Nope.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1522710</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522710</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Wed, 04 Jun 2008 17:49:22 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Thu, 05 Jun 2008 00:01:30 GMT]]></title><description><![CDATA[<p>Octree is working. Thank you!<br />
Here is a result: <a href="http://keep4u.ru/imgs/b/080605/6d/6d685b154a62067518.jpg" rel="nofollow">http://keep4u.ru/imgs/b/080605/6d/6d685b154a62067518.jpg</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1522871</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1522871</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 05 Jun 2008 00:01:30 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Thu, 05 Jun 2008 11:57:51 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>Octree is working. Thank you!<br />
Here is a result: <a href="http://keep4u.ru/imgs/b/080605/6d/6d685b154a62067518.jpg" rel="nofollow">http://keep4u.ru/imgs/b/080605/6d/6d685b154a62067518.jpg</a></p>
</blockquote>
<p>Hey. That looks nice. But normally the detailed part should be near to the camera or is this intented?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1523217</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1523217</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 05 Jun 2008 11:57:51 GMT</pubDate></item><item><title><![CDATA[Reply to Create array of objects with different constructor parameters on Thu, 05 Jun 2008 20:06:44 GMT]]></title><description><![CDATA[<p>Detalization depend on the values stored in mesh, not on the camera position.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1523664</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1523664</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 05 Jun 2008 20:06:44 GMT</pubDate></item></channel></rss>