<?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: 8.8 &#8212; Constructors (Part II)</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/</link>
	<description></description>
	<pubDate>Wed, 19 Nov 2008 17:28:52 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/#comment-17920</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Sat, 31 May 2008 02:20:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/#comment-17920</guid>
		<description>I have rewritten that portion of the lesson to reflect a more appropriate usage for private constructors.  Thanks for letting me know I made a mistake with this.</description>
		<content:encoded><![CDATA[<p>I have rewritten that portion of the lesson to reflect a more appropriate usage for private constructors.  Thanks for letting me know I made a mistake with this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cymkhat</title>
		<link>http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/#comment-17735</link>
		<dc:creator>cymkhat</dc:creator>
		<pubDate>Wed, 28 May 2008 11:09:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/#comment-17735</guid>
		<description>You dont need a private constructor (i.e a private constructor is NOT A MUST) to restrict an object being instantiated without arguments.

--If you want a object to be instantiated in a particular way (with the constructor which has arguments) : just declare/define an constructor that takes arguments and intializes the object in that particular way.

--YOU DO NOT NEED A PRIVATE CONSTRUCTOR FOR THAT

--look at the following code and try to compile it. it will give the following error. see that I did not declare a private constructor yet the compiler fails to compile as there exists a constructor with arguments.

&lt;pre&gt;

#include 
#include 
#include 
using namespace std;

enum CarSize{
        small = 0, medium = 1, large = 2
};

class RentalAccount{
        string accountNumber;
        string clientName;
        int age;
        float balance;

        public:
        // -- Constructor
        RentalAccount( const char* newAccountNumber,  const char* newClientName, const int newAge, const float newBalance = 0) { }

        // -- Set, and get Balance
        void SetBalance(const float newBalance);

        float GetBalance() const;

        // -- Calculate Balance
        void CalculateBalance(CarSize carSize);

        // -- Print Client information
        void Print() const;
};


int main()
{
        RentalAccount *C = new RentalAccount();

        return 1;
}

&lt;/pre&gt;

when you compile this - you will get the follwing error. Anyway the compiler is returning an error even though you I didn't declare a private constructor, so I dont think a private constructor is needed to prevent somebody from instantiating an object using a plain constructor without any arguments. just declare a constructor with arguments and the compiler will do the rest.

cymkhat@cymkhat-laptop:~/cpp$ g++ cons.cpp
cons.cpp: In function â€˜int main()â€™:
cons.cpp:35: error: no matching function for call to â€˜RentalAccount::RentalAccount()â€™
cons.cpp:18: note: candidates are: RentalAccount::RentalAccount(const char*, const char*, int, float)
cons.cpp:10: note:                 RentalAccount::RentalAccount(const RentalAccount&#38;)


my g++ version is

cymkhat@cymkhat-laptop:~/cpp$ g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)</description>
		<content:encoded><![CDATA[<p>You dont need a private constructor (i.e a private constructor is NOT A MUST) to restrict an object being instantiated without arguments.</p>
<p>&#8211;If you want a object to be instantiated in a particular way (with the constructor which has arguments) : just declare/define an constructor that takes arguments and intializes the object in that particular way.</p>
<p>&#8211;YOU DO NOT NEED A PRIVATE CONSTRUCTOR FOR THAT</p>
<p>&#8211;look at the following code and try to compile it. it will give the following error. see that I did not declare a private constructor yet the compiler fails to compile as there exists a constructor with arguments.</p>
<pre>

#include
#include
#include
using namespace std;

enum CarSize{
        small = 0, medium = 1, large = 2
};

class RentalAccount{
        string accountNumber;
        string clientName;
        int age;
        float balance;

        public:
        // -- Constructor
        RentalAccount( const char* newAccountNumber,  const char* newClientName, const int newAge, const float newBalance = 0) { }

        // -- Set, and get Balance
        void SetBalance(const float newBalance);

        float GetBalance() const;

        // -- Calculate Balance
        void CalculateBalance(CarSize carSize);

        // -- Print Client information
        void Print() const;
};

int main()
{
        RentalAccount *C = new RentalAccount();

        return 1;
}
</pre>
<p>when you compile this - you will get the follwing error. Anyway the compiler is returning an error even though you I didn&#8217;t declare a private constructor, so I dont think a private constructor is needed to prevent somebody from instantiating an object using a plain constructor without any arguments. just declare a constructor with arguments and the compiler will do the rest.</p>
<p>cymkhat@cymkhat-laptop:~/cpp$ g++ cons.cpp<br />
cons.cpp: In function â€˜int main()â€™:<br />
cons.cpp:35: error: no matching function for call to â€˜RentalAccount::RentalAccount()â€™<br />
cons.cpp:18: note: candidates are: RentalAccount::RentalAccount(const char*, const char*, int, float)<br />
cons.cpp:10: note:                 RentalAccount::RentalAccount(const RentalAccount&amp;)</p>
<p>my g++ version is</p>
<p>cymkhat@cymkhat-laptop:~/cpp$ g++ -v<br />
Using built-in specs.<br />
Target: i486-linux-gnu<br />
Configured with: ../src/configure -v &#8211;enable-languages=c,c++,fortran,objc,obj-c++,treelang &#8211;prefix=/usr &#8211;enable-shared &#8211;with-system-zlib &#8211;libexecdir=/usr/lib &#8211;without-included-gettext &#8211;enable-threads=posix &#8211;enable-nls &#8211;with-gxx-include-dir=/usr/include/c++/4.2 &#8211;program-suffix=-4.2 &#8211;enable-clocale=gnu &#8211;enable-libstdcxx-debug &#8211;enable-objc-gc &#8211;enable-mpfr &#8211;enable-targets=all &#8211;enable-checking=release &#8211;build=i486-linux-gnu &#8211;host=i486-linux-gnu &#8211;target=i486-linux-gnu<br />
Thread model: posix<br />
gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Learn C++ - &#187; 8.7 &#8212; The hidden &#8220;this&#8221; pointer</title>
		<link>http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/#comment-14394</link>
		<dc:creator>Learn C++ - &#187; 8.7 &#8212; The hidden &#8220;this&#8221; pointer</dc:creator>
		<pubDate>Mon, 05 May 2008 01:57:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/#comment-14394</guid>
		<description>[...] 2007      Prev/Next Posts   &#171; 8.6 &#8212; Destructors &#124; Home &#124; 8.8 &#8212; Constructors (Part II) &#187;     Thursday, September 6th, 2007 at 10:20 [...]</description>
		<content:encoded><![CDATA[<p>[...] 2007      Prev/Next Posts   &laquo; 8.6 &#8212; Destructors | Home | 8.8 &#8212; Constructors (Part II) &raquo;     Thursday, September 6th, 2007 at 10:20 [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/#comment-10562</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Sat, 29 Mar 2008 21:00:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/#comment-10562</guid>
		<description>First, make sure the constructor sets the pointer to NULL if it's not going to allocate memory to the pointer.  Failure to do so will cause a problem somewhere along the line.

Second, before allocating memory in Init(), check to see if the pointer is non-NULL.  If it's non-NULL, then Init() has probably already been called, and no allocation is needed.  If it's NULL, then Init() can do the allocation.</description>
		<content:encoded><![CDATA[<p>First, make sure the constructor sets the pointer to NULL if it&#8217;s not going to allocate memory to the pointer.  Failure to do so will cause a problem somewhere along the line.</p>
<p>Second, before allocating memory in Init(), check to see if the pointer is non-NULL.  If it&#8217;s non-NULL, then Init() has probably already been called, and no allocation is needed.  If it&#8217;s NULL, then Init() can do the allocation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom</title>
		<link>http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/#comment-10533</link>
		<dc:creator>Tom</dc:creator>
		<pubDate>Sat, 29 Mar 2008 07:45:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/88-constructors-part-ii/#comment-10533</guid>
		<description>&lt;b&gt;"be careful when using Init() functions and dynamically allocated memory. Because Init() functions can be called by anyone at any time, dynamically allocated memory may or may not have already been allocated when Init() is called. Be careful to handle this situation appropriately â€” it can be slightly confusing, since a non-null pointer could be either dynamically allocated memory or an uninitialized pointer!"&lt;/b&gt;

Alex -

So what's the correct way to handle the situation? Is it as simple as making sure to set the dynamic memory pointer to NULL (or to a valid address) before the Init() functions can be called?

Thanks!</description>
		<content:encoded><![CDATA[<p><b>&#8220;be careful when using Init() functions and dynamically allocated memory. Because Init() functions can be called by anyone at any time, dynamically allocated memory may or may not have already been allocated when Init() is called. Be careful to handle this situation appropriately â€” it can be slightly confusing, since a non-null pointer could be either dynamically allocated memory or an uninitialized pointer!&#8221;</b></p>
<p>Alex -</p>
<p>So what&#8217;s the correct way to handle the situation? Is it as simple as making sure to set the dynamic memory pointer to NULL (or to a valid address) before the Init() functions can be called?</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
