<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: 10.1 &#8212; Constructor initialization lists</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/</link>
	<description></description>
	<pubDate>Thu, 28 Aug 2008 18:38:58 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Daniel</title>
		<link>http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/#comment-18722</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Thu, 12 Jun 2008 00:41:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/#comment-18722</guid>
		<description>Thanks, Daniel</description>
		<content:encoded><![CDATA[<p>Thanks, Daniel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/#comment-18613</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Tue, 10 Jun 2008 02:52:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/#comment-18613</guid>
		<description>Unfortunately, neither of those work.  This actually hits on one of the shortcomings of the C++ language -- there is no way to initialize non-static array members in C++.

However, there is a reasonable workaround.  Because this is a constant array, we know the value of the members will never change -- so why even have one array per class object?  It makes more sense to define m_nValue as a static member, so it will be shared by all class objects.  That way they all reference the same one and you save memory.  And there IS syntax to initialize static array members in C++:

&lt;pre&gt;
class Something
{
private:
    static const int m_nValue[3];
public:
    Something()
    {
    }
};

const int Something::m_nValue[] = {5, 6, 7}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Unfortunately, neither of those work.  This actually hits on one of the shortcomings of the C++ language &#8212; there is no way to initialize non-static array members in C++.</p>
<p>However, there is a reasonable workaround.  Because this is a constant array, we know the value of the members will never change &#8212; so why even have one array per class object?  It makes more sense to define m_nValue as a static member, so it will be shared by all class objects.  That way they all reference the same one and you save memory.  And there IS syntax to initialize static array members in C++:</p>
<pre>
class Something
{
private:
    static const int m_nValue[3];
public:
    Something()
    {
    }
};

const int Something::m_nValue[] = {5, 6, 7}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/#comment-18301</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Thu, 05 Jun 2008 00:20:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/#comment-18301</guid>
		<description>Hi Alex,

Just a question, can you use initialisation lists with arrays of members? something like this:&lt;pre&gt;class Something
{
private:
    const int m_nValue[3];
public:
    Something(): m_nValue(5, 6, 7)
    {
    }
};&lt;/pre&gt;Or do you have to do it like this:&lt;pre&gt;class Something
{
private:
    const int m_nValue[3];
public:
    Something(): m_nValue[0](5), m_nValue[1](6), m_nValue[2](7)
    {
    }
};&lt;/pre&gt;Thanks,
Daniel</description>
		<content:encoded><![CDATA[<p>Hi Alex,</p>
<p>Just a question, can you use initialisation lists with arrays of members? something like this:
<pre>class Something
{
private:
    const int m_nValue[3];
public:
    Something(): m_nValue(5, 6, 7)
    {
    }
};</pre>
<p>Or do you have to do it like this:
<pre>class Something
{
private:
    const int m_nValue[3];
public:
    Something(): m_nValue[0](5), m_nValue[1](6), m_nValue[2](7)
    {
    }
};</pre>
<p>Thanks,<br />
Daniel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Learn C++ - &#187; 9.12 &#8212; Shallow vs. deep copying</title>
		<link>http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/#comment-14413</link>
		<dc:creator>Learn C++ - &#187; 9.12 &#8212; Shallow vs. deep copying</dc:creator>
		<pubDate>Mon, 05 May 2008 02:12:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/#comment-14413</guid>
		<description>[...]  10.1 â€” Constructor initialization lists [...]</description>
		<content:encoded><![CDATA[<p>[...]  10.1 â€” Constructor initialization lists [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Learn C++ - &#187; 10.2 -- Composition</title>
		<link>http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/#comment-3879</link>
		<dc:creator>Learn C++ - &#187; 10.2 -- Composition</dc:creator>
		<pubDate>Tue, 04 Dec 2007 22:55:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/#comment-3879</guid>
		<description>[...] the previous lesson on initializer lists, you learned that the preferred way to initialize class members is through initializer lists rather [...]</description>
		<content:encoded><![CDATA[<p>[...] the previous lesson on initializer lists, you learned that the preferred way to initialize class members is through initializer lists rather [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
