<?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[Can not call constructors of inherited objects]]></title><description><![CDATA[<p>Hello!</p>
<p>I have the following</p>
<pre><code class="language-cpp">class A
{
public:
  struct {
    B b; //B and C -- some classes which can be bitwise copied,
    C c; //  thus I do not need in copy constructor of this structure
  } dataContainer;

  D d; 

  A(void):dataContainer.b(1.0),dataContainer.c('a'),d(-3){;} //Call constructors of inherited objects -- error here :(
}
</code></pre>
<p>I put some members to the <code>dataContainer</code> structure, because I can need, for example, to make a temporary copy of members from <code>dataContainer</code> , or pass these members to some function etc.</p>
<p>But compiler do not like the <code>dataContainer.b(1.0)</code> . I hope that this code will skip the <code>dataContainer</code> default constructor (because I do not call it in <code>A</code> constructor) and instead will directly construct <code>dataContainer.b</code> and <code>dataContainer.c</code> with initial values I need.</p>
<p>Another question: what to do with anonymous struct:</p>
<pre><code class="language-cpp">{
public:
  struct {
    B b; //B and C -- some classes which can be bitwise copied,
    C c; //  thus I do not need in copy constructor of this structure
  }; //Anonymous struct, all OK here

  D d; 

  A(void):b(1.0),c('a'),d(-3){;} //Is this OK?
}
</code></pre>
<p>How to make constructor of anonymous struct?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/212080/can-not-call-constructors-of-inherited-objects</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 14:15:24 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/212080.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 01 May 2008 08:34:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Can not call constructors of inherited objects on Thu, 01 May 2008 08:36:00 GMT]]></title><description><![CDATA[<p>Hello!</p>
<p>I have the following</p>
<pre><code class="language-cpp">class A
{
public:
  struct {
    B b; //B and C -- some classes which can be bitwise copied,
    C c; //  thus I do not need in copy constructor of this structure
  } dataContainer;

  D d; 

  A(void):dataContainer.b(1.0),dataContainer.c('a'),d(-3){;} //Call constructors of inherited objects -- error here :(
}
</code></pre>
<p>I put some members to the <code>dataContainer</code> structure, because I can need, for example, to make a temporary copy of members from <code>dataContainer</code> , or pass these members to some function etc.</p>
<p>But compiler do not like the <code>dataContainer.b(1.0)</code> . I hope that this code will skip the <code>dataContainer</code> default constructor (because I do not call it in <code>A</code> constructor) and instead will directly construct <code>dataContainer.b</code> and <code>dataContainer.c</code> with initial values I need.</p>
<p>Another question: what to do with anonymous struct:</p>
<pre><code class="language-cpp">{
public:
  struct {
    B b; //B and C -- some classes which can be bitwise copied,
    C c; //  thus I do not need in copy constructor of this structure
  }; //Anonymous struct, all OK here

  D d; 

  A(void):b(1.0),c('a'),d(-3){;} //Is this OK?
}
</code></pre>
<p>How to make constructor of anonymous struct?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1501536</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1501536</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 01 May 2008 08:36:00 GMT</pubDate></item><item><title><![CDATA[Reply to Can not call constructors of inherited objects on Thu, 01 May 2008 09:21:54 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>Hello!</p>
<p>I have the following</p>
<pre><code class="language-cpp">class A
{
public:
  struct {
    B b; //B and C -- some classes which can be bitwise copied,
    C c; //  thus I do not need in copy constructor of this structure
  } dataContainer;

  D d; 

  A(void):dataContainer.b(1.0),dataContainer.c('a'),d(-3){;} //Call constructors of inherited objects -- error here :(
}
</code></pre>
<p>I put some members to the <code>dataContainer</code> structure, because I can need, for example, to make a temporary copy of members from <code>dataContainer</code> , or pass these members to some function etc.</p>
</blockquote>
<p>Being an unnamed structure, it cannot be used to declare a function parameter or return type - this also means that you cannot create a function to serve as a pseudo-constructor (except perhaps a template function through type deduction, but we'll ignore that here). Giving the struct a name should solve the problem, you could then declare a proper constructor for it or use a function instead (that it may stay a POD if necessary).</p>
<pre><code class="language-cpp">class A
{
public:
  struct dataContainerType {
    dataContainerType(const B&amp; b,const C&amp; c) : b(b), c(c) {} // use constructor or
    static dataContainerType make(const B&amp; b,const C&amp; c) { dataContainerType x = { b,c }; return x; } // ordinary function
    B b;
    C c;
  } dataContainer;

  D d; 

  A(void):dataContainer(dataContainerType::make(1.0,'a'))
}
</code></pre>
<p>Either soultion is equally efficient, as the copy that would have to be made within the function make may be eliminated at the compiler's discretion (NRVO).<br />
As for terms, b and c are not <em>inherited</em> members - that would imply that you dervied from them.</p>
<blockquote>
<p>Another question: what to do with anonymous struct:</p>
<pre><code class="language-cpp">{
public:
  struct {
    B b; //B and C -- some classes which can be bitwise copied,
    C c; //  thus I do not need in copy constructor of this structure
  }; //Anonymous struct, all OK here

  D d; 

  A(void):b(1.0),c('a'),d(-3){;} //Is this OK?
}
</code></pre>
<p>How to make constructor of anonymous struct?</p>
</blockquote>
<p>There are no <em>anonymous</em> structs (that is, declaring an <em>unnamed</em> struct without declaring an object of that type or a derived type or declaring a typedef name at the same time) in Standard C++. And - as indicated above - an <em>unnamed</em> class cannot have user declared constructors or destructors.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1501543</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1501543</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 01 May 2008 09:21:54 GMT</pubDate></item><item><title><![CDATA[Reply to Can not call constructors of inherited objects on Thu, 01 May 2008 09:38:34 GMT]]></title><description><![CDATA[<p>Thank you!<br />
I understand.</p>
<p>Can I write this way: <code>A(void):dataContainer({1.0,'a'}),d(-3){}</code> ?</p>
<p>Another problem is that in my code I have the following:</p>
<pre><code class="language-cpp">template&lt;_TTP&gt; _Cell&lt;_CTP&gt;::_Cell(_meshPointer const mesh):
  Structure.parent(NULL),Structure.children(NULL),Structure.fixed(false),Structure.numberOfLeafs(0),
  Structure.newCell(false),Structure.Adress(mesh),Structure.unsplitable(false),Structure.unmergeable(false),[...]
</code></pre>
<p>When I <em>reading</em> this code, I see, that the newly constructed cell has no parent, has no children, structure is not fixed and so on...</p>
<p>When I fix it, it becomes completely <em>unreadable</em>:</p>
<pre><code class="language-cpp">template&lt;_TTP&gt; _Cell&lt;_CTP&gt;::_Cell(_meshPointer const mesh):
  Structure(NULL,NULL,false,0,false,mesh,false,false,[...]), [...]
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1501546</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1501546</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 01 May 2008 09:38:34 GMT</pubDate></item><item><title><![CDATA[Reply to Can not call constructors of inherited objects on Thu, 01 May 2008 11:59:53 GMT]]></title><description><![CDATA[<p>If you want it readable in this way, you have to put the assignments in the constructor block, not in its initialization list:</p>
<pre><code class="language-cpp">template&lt;_TTP&gt; _Cell&lt;_CTP&gt;::_Cell(_meshPointer const mesh)
{
  Structure.parent = NULL;
  Structure.children = NULL;
  Structure.fixed = false;
  //...
}
</code></pre>
<p>Another way are the so called &quot;named constructors&quot;:<br />
<a href="http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.8" rel="nofollow">http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.8</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1501592</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1501592</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Thu, 01 May 2008 11:59:53 GMT</pubDate></item><item><title><![CDATA[Reply to Can not call constructors of inherited objects on Thu, 01 May 2008 15:21:56 GMT]]></title><description><![CDATA[<p>The approach of Th69 is ok if Structure's members are of types without nontrivial ctors. Having types with nontrivial ctors in Structure that approach would give a default-construction of that member during initialization and a copy-assignment in Cell's ctor body.<br />
For structures or classes that have such a long ctor argument list as Structure in your example, one could gain some readability by defining extra types or functions for each (or most) of the ctor arguments.<br />
The definition of an extra type could look like this:</p>
<pre><code class="language-cpp">struct StrucParent {
  S* par_;   //I don't know what type the parent is, replace that S appropiately
  StrucParent(S* par) : par_(par) {};
};
</code></pre>
<p>and then replace the first argument of Structure's ctor with a StrucParent:</p>
<pre><code class="language-cpp">class Structure {
/* ... */
  // Structure(S* parent , *....  //replaced
  Structure (StrucParent parent, /*...*/);
};
</code></pre>
<p>And then call the Ctor like this:</p>
<pre><code class="language-cpp">template&lt;_TTP&gt; _Cell&lt;_CTP&gt;::_Cell(_meshPointer const mesh) :
  Structure( StrucParent(NULL),
             StrucChildren(NULL),
             StrucFixed(false), /*...*/
           ), [...]
</code></pre>
<p>If you cannot (or do not want to) modify Structure's ctor, you could provide some simple inline functions like this:</p>
<pre><code class="language-cpp">inline S* StrucParent(S* par) {return par;}
inline bool StrucFixed(bool f) {return f;}
// etc.
</code></pre>
<p>And calling the ctor like stated above - but this time the old ctor as you had it before. Here the readability is better than in the original example, without any performance overhead (I'm pretty sure that any compiler will toss away the pass-through functions). The only flaw is that you pollute your surrounding namespace with the names of those functions. That could be solved like that:</p>
<pre><code class="language-cpp">//Structure.hpp
namespace Struc {
  inline S* Parent(S* par) {return par;}
  inline bool Fixed(bool f) {return f;}
  //etc.
}

//Cell.hpp
template&lt;_TTP&gt; _Cell&lt;_CTP&gt;::_Cell(_meshPointer const mesh) :
  Structure( Struc::Parent(NULL),
             Struc::Children(NULL),
             Struc::Fixed(false), /*...*/
           ), [...]
</code></pre>
<p>Enough readability? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>PS: an advantage of the define-new-types-version is, that you can provide ctors of Structure where you leave out different arguments with the same type - thyt otherwise would be ambigous:</p>
<pre><code class="language-cpp">Structure::Structure(StrucParent, StrucFixed);    //1) ok, presumably children will be set to NULL
Structure::Structure(StrucChildren, StrucFixed); //2) this time parent = NULL

Structure(S* parent, bool fixed);  //3) same as 1 without new types.
Structure(S* children, bool fixed); //4) intended to be same as 2) but is abiguous with 3)!!
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1501674</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1501674</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Thu, 01 May 2008 15:21:56 GMT</pubDate></item><item><title><![CDATA[Reply to Can not call constructors of inherited objects on Mon, 12 May 2008 09:36:40 GMT]]></title><description><![CDATA[<p>Closed thread because it is a spam bot friend <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1507339</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1507339</guid><dc:creator><![CDATA[evilissimo]]></dc:creator><pubDate>Mon, 12 May 2008 09:36:40 GMT</pubDate></item></channel></rss>