<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C++ Source</title>
	<atom:link href="http://cppsource.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://cppsource.com</link>
	<description>Your Source for C++ Information</description>
	<lastBuildDate>Wed, 12 Aug 2009 18:20:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Namespaces</title>
		<link>http://cppsource.com/2009/08/namespaces/</link>
		<comments>http://cppsource.com/2009/08/namespaces/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 18:16:45 +0000</pubDate>
		<dc:creator>carlos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cppsource.com/?p=79</guid>
		<description><![CDATA[A namespace is a feature that allows classes and functions to be created in a restricted scope, that is, accessible only using the name of the namespace as a qualifier.
Namespaces exist to simplify the task of giving names to classes and functions. In C, all structs and functions are in the global namespace (unless they [...]]]></description>
			<content:encoded><![CDATA[<p>A namespace is a feature that allows classes and functions to be created in a restricted scope, that is, accessible only using the name of the namespace as a qualifier.</p>
<p>Namespaces exist to simplify the task of giving names to classes and functions. In C, all structs and functions are in the global namespace (unless they are made static). This means that it is very possible that a clash may happen between methods or structs defined in different libraries.</p>
<p>The most common method used to solve this problem in C and earlier versions of C++ was creating prefixes. For example, if a group developed a library called YZ, they would prefix all symbols define by this library with the YZ_ prefix. Therefore, this would prevent a clash with other symbols defined in different libraries.</p>
<p>Modern languages avoid this problem by making all code part of a module, where definitions of symbols are maintained. Symbols can only be referenced by importing a module, according to the rules of the language. For example, Java has packages, C# has assemblies, and Python has modules.</p>
<p>In C++, namespaces are not required, but are strongly encouraged for new development. Using namespaces means that there are less opportunities for clashes with software that was written by other people.</p>
<hr />
<h2><a name="_examples"></a>Examples</h2>
<p>Here is an example of how to define a namespace called &#8220;Example&#8221;:</p>
<table border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10">
<tr>
<td>
<pre>namespace Example {
  class A {
   void test();
  }
}</pre>
</td>
</tr>
</table>
<p>Code can be added to a namespace by simply using the namespace block as show above:</p>
<table border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10">
<tr>
<td>
<pre>namespace Example {
void A::test() { std::cout &lt;&lt; " a test \n"; }
}</pre>
</td>
</tr>
</table>
<p>To use a class defined in a namespace, you can refer to the fully qualified name (namespace + class name):</p>
<table border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10">
<tr>
<td>
<pre>void test2() {
   Example::A a = new Example::A();
}</pre>
</td>
</tr>
</table>
<p>The second possibility is employing the <tt>using</tt> clause:</p>
<table border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10">
<tr>
<td>
<pre>using namespace Example;
void test3() {
   A a = new A();
}</pre>
</td>
</tr>
</table>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcppsource.com%2F2009%2F08%2Fnamespaces%2F&amp;linkname=Namespaces"><img src="http://cppsource.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://cppsource.com/2009/08/namespaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exceptions Inside Exceptions</title>
		<link>http://cppsource.com/2009/08/exceptions-inside-exceptions/</link>
		<comments>http://cppsource.com/2009/08/exceptions-inside-exceptions/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 19:59:22 +0000</pubDate>
		<dc:creator>carlos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cppsource.com/?p=77</guid>
		<description><![CDATA[One of the risks that we incur when working with exceptions, is having to control what is happening when the exception is received in the catch block.
An exception is thrown, as we have seen in the article about exceptions when the keyword throw is used. Then, a new object of the time determining the exception [...]]]></description>
			<content:encoded><![CDATA[<p>One of the risks that we incur when working with exceptions, is having to control what is happening when the exception is received in the catch block.</p>
<p>An exception is thrown, as we have seen in the <a href="http://cppsource.com/2009/07/exceptions/">article about exceptions</a> when the keyword <tt>throw</tt> is used. Then, a new object of the time determining the exception is created. For  example, we can create a new exception in the following way:</p>
<table border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10">
<tr>
<td>
<pre>void Obj::AMethod() {
   // ....
   if (error)
      throw new MyCustomException("something bad happened");
}</pre>
</td>
</tr>
</table>
<p>An exception is catch by a <tt>catch</tt> block that can be added to any code surrounding the place where the exception was thrown. Surrounding here means not only the code that directly throws the exception, but also any code that called the method where the exception is being thrown.</p>
<p>Here is an example:</p>
<table border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10">
<tr>
<td>
<pre>void Obj::Example() {
   try {
   // ...
      this-&gt;AMethod();
   } catch (MyCustomException &amp;e) {
      // process the exception here
   }
}</pre>
</td>
</tr>
</table>
<p>The main problem we are concerned about here is what happens between the moment the exception is thrown and the moment the program can restart on the catch block.</p>
<p>The main problem with catching exceptions, is that we need to make sure that no additional exceptions will be thrown during the process of destroying objects. For example, if the code above has an object with a destructor that throws another exception, then we have a problem.</p>
<p>Notice that, when throwing an exception, C++ maintains a context, which determines what destructor will be called in response to the exception. The fact is that all objects created in the context between the place where an exception was thrown until the catch block need to be destroyed.</p>
<p>If we send an exception during the destruction process, then C++ can become confused about what objects needs to be destroyed as a result. It is as if there were are double queue of objects that need to have their destructor called.</p>
<p>In such a situation, C++ won&#8217;t know what to do, and the program will simply stop working correctly and crash.</p>
<p>To avoid problems like this, the main suggestion is that we should avoid throwing exceptions on a destructor. We never know in what context a destructor will be called, so it is better to avoid throwing exception on any destructor.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcppsource.com%2F2009%2F08%2Fexceptions-inside-exceptions%2F&amp;linkname=Exceptions%20Inside%20Exceptions"><img src="http://cppsource.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://cppsource.com/2009/08/exceptions-inside-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exceptions</title>
		<link>http://cppsource.com/2009/07/exceptions/</link>
		<comments>http://cppsource.com/2009/07/exceptions/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 20:39:29 +0000</pubDate>
		<dc:creator>carlos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cppsource.com/?p=65</guid>
		<description><![CDATA[An exception is an indication that something unexpected occurred in a C program. The mechanism used by C to handle exceptions is using the try/catch pair of keywords.
An exception is represented in C++ by an object that encodes the condition that caused the exceptional event. One can use this object by means of the throw [...]]]></description>
			<content:encoded><![CDATA[<p>An exception is an indication that something unexpected occurred in a C<tt> program. The mechanism used by C</tt> to handle exceptions is using the try/catch pair of keywords.</p>
<p>An exception is represented in C++ by an object that encodes the condition that caused the exceptional event. One can use this object by means of the throw keyword.</p>
<p>The try/catch mechanism allows one to create a context in which we can protect against exceptions. For example</p>
<table border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10">
<tr>
<td>
<pre>class MyException { /* ... */ }
int i = 0;
try {
  i = performCalculation();
}
catch (MyException &amp;e) {
   cout &lt;&lt; "an exception occurred\n";
}</pre>
</td>
</tr>
</table>
<p>Observations: * note that in the catch clause, the exception is taken by reference. This is useful to avoid the need of copying the object.</p>
<p>Once you detect an exceptional condition, the exception can be throw using code like this:</p>
<table border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10">
<tr>
<td>
<pre>if (conditionHappened)
  throw new MyException();</pre>
</td>
</tr>
</table>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcppsource.com%2F2009%2F07%2Fexceptions%2F&amp;linkname=Exceptions"><img src="http://cppsource.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://cppsource.com/2009/07/exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Variables in C++</title>
		<link>http://cppsource.com/2009/07/variables-in-c/</link>
		<comments>http://cppsource.com/2009/07/variables-in-c/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:34:49 +0000</pubDate>
		<dc:creator>carlos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cppsource.com/2009/07/variables-in-c/</guid>
		<description><![CDATA[Programming is all about creating change in the way information is stored in a computer. To make this possible, we need easy access to the contents of computers memory. This is what programming language variables allow us to do: they give a simple way to store and retrieve information into the computer&#8217;s memory.

It may come [...]]]></description>
			<content:encoded><![CDATA[<p>Programming is all about creating change in the way information is stored in a computer. To make this possible, we need easy access to the contents of computers memory. This is what programming language variables allow us to do: they give a simple way to store and retrieve information into the computer&#8217;s memory.
</p>
<p>It may come as a surprise to you that we, as programmers of high level applications, have direct access only to locations in the random access memory (RAM). Whenever it is necessary to have access to disks, the network servers, or other types of store memory, this is done in an indirect way, through manipulation of main memory.
</p>
<p>A variable is a named memory, that can be used to read and write information. In this sense, variables are nothing more than &#8220;buckets&#8221; that can be used to store whatever values are needed for a specific computation. However, as different materials require different containers, we also have different types of variables for the various contents that can be stored in it.
</p>
<p>Suppose, for example, that the value to be stored is an integer number. In C++ this is called an int, and there is a specific declaration for each variable of the integer type, so the computer knows  that only integers will be stored in that location.
</p>
<h2>Types of variables<br />
</h2>
<p>There are just a few basic variable types in C++. They can be either numeric (such as integers and floating point numbers) , characters (such as the letters of the alphabet), or Booleans (representing the values true or false). What makes C++ rich is the several ways in which these basic building blocks can be combined. For example, one can have sequences of variables, variables that can be negative (signed) or only non-negative (unsigned),  or variables that have more or less space to store values (short or long). Moreover, C++ allows these basic variable types to be combined into complex data objects, as we will see later in this book.
</p>
<p>
 </p>
<h2>The basic types are the following:<br />
</h2>
<p><strong>Integers</strong>: they are declared using the keyword int. They represent integer number that are positive or negative. The limit on the integer size is dictated by the architecture in which the compiler has been implemented. Many modern computers store integers in four bytes, thus 2^32 is a practical limit for the values that can be stored in an integer variable.
</p>
<p><strong>Floating point numbers</strong>: Floating point numbers are used to represent real numbers in an approximate way. Remember that real numbers may have an infinite representation (for example, Pi), therefore only approximations can be stored in a computer. C++, as its antecessor C, has two main types for floating point numbers: float and double. A double is supposed to have more storage capacity than floats, and this is usually the case depending on the computer used. Although both types are available, its considered better to used only doubles to avoid rounding problems.
</p>
<p><strong>Characters</strong>: a character, declared as a char, can be used to store any alphanumeric values, such as the letters from &#8216;a&#8217; to &#8216;z&#8217;, numeric values from &#8216;0&#8242; to &#8216;9&#8242; when they are not interpreted as numbers, and other codes such as punctuation symbols, etc.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcppsource.com%2F2009%2F07%2Fvariables-in-c%2F&amp;linkname=Variables%20in%20C%2B%2B"><img src="http://cppsource.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://cppsource.com/2009/07/variables-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When Should You Create A Virtual Function?</title>
		<link>http://cppsource.com/2009/06/when-should-you-create-a-virtual-function/</link>
		<comments>http://cppsource.com/2009/06/when-should-you-create-a-virtual-function/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 14:58:19 +0000</pubDate>
		<dc:creator>carlos</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://cppsource.com/?p=55</guid>
		<description><![CDATA[One of the questions raised by people starting to use C++ and virtual functions is: when should I use virtual functions?
To answer this question, you need to understand the reason why virtual functions exist in the first place.
A virtual function is a mechanism used for polymorphism in the C++ language. The standard is example is [...]]]></description>
			<content:encoded><![CDATA[<p>One of the questions raised by people starting to use C++ and virtual functions is: when should I use virtual functions?</p>
<p>To answer this question, you need to understand the reason why virtual functions exist in the first place.</p>
<p>A virtual function is a mechanism used for polymorphism in the C++ language. The standard is example is the drawing method of a shape class.
<pre class="code">
class Shape { public virtual void draw(); }
class Rectangle : public Shape {
  public virtual void draw();
}
</pre>
<p>What is happening here is that the&nbsp;<strong>draw</strong> method&nbsp;varies between Shape and Rectangle. Therefore, it doesn&#8217;t make sense to have only one implementation. You need to have one version of draw for each of the classes Shape and Rectangle.</p>
<p>Whenever you have a situation where classes in the same hierarchy need different implementations, then <strong>virtual</strong> is the keyword you need to use. With virtual we are free to redefine a member function on a derived class.</p>
<p>On the other hand, member functions that were not defined with the virtual keyword cannot be redefined by derived classes. The reason for that is that, since &#8220;virtual&#8221; is not used,&nbsp;the compiler doesn&#8217;t know about different versions of the member function on the derived classes. Therefore, the compiler will call the <strong>wrong version </strong>of&nbsp;the member function.<br />
<h2>General Techniques</h2>
<p>Programmers diverge on the general technique used to say if a method will be virtual or not. Some people believe that you should make all, or at least most of your methods virtual. The idea is that you never know what might be useful to override later, so it is better to leave the options open.</p>
<p>The other group of developers believe that it is better to define only a few member functions as virtual. The reason is that then you will have more control on what the derived classes can do.</p>
<p>The arguments on both sides are good, and as most things in programming, it comes to choosing a side and sticking to it.<br />
<h2>Conclusion</h2>
<p>Virtual functions are the main mechanism that allows different behavior for classes in the same hierarchy. In this way, we should be careful in defining what virtual functions are virtual and what are not. Understanding the virtual keyword you have better ways to defining correct behavior in your classes.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcppsource.com%2F2009%2F06%2Fwhen-should-you-create-a-virtual-function%2F&amp;linkname=When%20Should%20You%20Create%20A%20Virtual%20Function%3F"><img src="http://cppsource.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://cppsource.com/2009/06/when-should-you-create-a-virtual-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Virtual Functions in C++</title>
		<link>http://cppsource.com/2009/04/virtual-functions-in-c/</link>
		<comments>http://cppsource.com/2009/04/virtual-functions-in-c/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 15:12:16 +0000</pubDate>
		<dc:creator>carlos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cppsource.com/?p=49</guid>
		<description><![CDATA[Object oriented programming is a style of programming that requires that functionality be attached to objects, instead of freely available through functions. C++, although not being a pure object oriented language, supports the notion of objects and classes of objects.
One of the big advantages of programming with objects is that they can behave differently based [...]]]></description>
			<content:encoded><![CDATA[<p>Object oriented programming is a style of programming that requires that functionality be attached to objects, instead of freely available through functions. C++, although not being a pure object oriented language, supports the notion of objects and classes of objects.</p>
<p>One of the big advantages of programming with objects is that they can behave differently based on who the messages are sent to. This kind of dynamic behavior is called polymorphism, and is implemented in C++ by the virtual function mechanism.</p>
<hr />
<h2><a name="_what_is_a_virtual_function"></a>What is a virtual function?</h2>
<p>A virtual function is a member of a class that can be overriden by a derived class to provide different functionality.</p>
<p>For example, a member function <tt>draw</tt> can be used to determine how a graphical object is drawn on the screen. Depending on the concrete type of the object, however, the shape of the object may change, and the way it is drawn will be different. The function <tt>draw</tt> in this case is a candidate to be implemented as a virtual function, which will have different implementations for each of the concrete classes.</p>
<hr />
<h2><a name="_creating_virtual_functions_in_c"></a>Creating virtual functions in C++</h2>
<p>A virtual function can be created in C++ with the use of the virtual keyword before the declaration of the member function. For example:</p>
<table border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10">
<tr>
<td>
<pre>class Button {
  virtual void draw() {
    // drawing a generic button
  }
};

class RoundButton {
  virtual void draw() {
    // draw button with a different shape
  }
}</pre>
</td>
</tr>
</table>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcppsource.com%2F2009%2F04%2Fvirtual-functions-in-c%2F&amp;linkname=Virtual%20Functions%20in%20C%2B%2B"><img src="http://cppsource.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://cppsource.com/2009/04/virtual-functions-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class Access Levels</title>
		<link>http://cppsource.com/2009/04/class-access-levels/</link>
		<comments>http://cppsource.com/2009/04/class-access-levels/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 15:47:55 +0000</pubDate>
		<dc:creator>carlos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cppsource.com/?p=35</guid>
		<description><![CDATA[C++ provides three access levels for members of a class. Both member variables and member functions can be tagged as public, private, or protected, depending on the intended use of the member in the class.
A public data member of member function is one that can be accessed by any user of the class, independent of [...]]]></description>
			<content:encoded><![CDATA[<p>C++ provides three access levels for members of a class. Both member variables and member functions can be tagged as public, private, or protected, depending on the intended use of the member in the class.</p>
<p>A public data member of member function is one that can be accessed by any user of the class, independent of where the user is located. Thus, member functions that are declared public can be accessed anywhere, and represent the public interface of a C++ class.</p>
<p>Public member variables are also allowed to be viewed anywhere in the program. However, allowing this to happen is usually not a good idea, since it would inibit future changes in the representation of the data stored in a class. Unless you really need to do this for some reason (for example, compatibility with C code), you should avoid the creation of public member variables in a C++ class.</p>
<p>Private member variables, on the other hand, are not visible outside the class where they are declared. This is the preferred way of declaring member variables in C++, since it guarantees that the usage of data is limited to the class itself.</p>
<p>Like private member variables, private member functions are also useful in C++ programs. Such member functions are used in the implementation of the class, and are not accessible to users of the class. Every member function that is not part of the interface should be made private (or at least protected).</p>
<p>In fact, C++ defines private access as the default level of access for class member. That is, if you don&#8217;t specify anything as the access level, then the member will be considered private.</p>
<p>The other level of accessibility is the protected level. A member function or member variable is protected if it is accessible only inside a class or by the classes that derive from it. Therefore, only classes that inherit from the class where the member is contained can have access to the function or variable.</p>
<p>Protected members of a class are useful to provide functionality for derived classes. However, they should be used with care. One of the main problems, is that classes that expose too many details to derived classes may have difficulties when changing their internal representation, since an unspecified number of derivate classes may be using the protected information.</p>
<hr />
<h2><a name="_example"></a>Example</h2>
<p>Here is an example of the access level described above. Note that the method Director::getInfo() incorrectly tries to access getAge, since it is a private method.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10">
<tr>
<td>
<pre>class Employee {
  // private member variable
  int age;
  // private member function
  int getAge() { return age; }
public:
  // public member variable (don't do this!)
  std::string name;
  std::string getName() { return name; }
protected:
  // these members are visible only inside Employee
  // and derived classes
  std::string telephone;
  std::string getTelephone() { return telephone; }

};

// A derived class
class Director : public Employee {
   public:
   void getInfo();
};

void Director::getInfo() {
   // this works - protected
   cout &lt;&lt; getTelephone() &lt;&lt; "\n";
   // this doesn't work - private to Employee
   cout &lt;&lt; getAge() &lt;&lt; "\n";
}</pre>
</td>
</tr>
</table>
<table frame="void" cellpadding="8">
<tr valign="top">
<td>
<p><b><u>Important</u></b></p>
</td>
<td style="border-left: 1px solid silver;">Don&#8217;t forget to add the public: tag before public members. Remember that the default access level in C++ is private.</td>
</tr>
</table>
<ul>
<li>Buy <a href="http://www.amazon.com/gp/product/0201700735?ie=UTF8&#038;tag=bookdablogonb-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0201700735">The C++ Programming Language: Special Edition (3rd Edition)</a><img src="http://www.assoc-amazon.com/e/ir?t=bookdablogonb-20&#038;l=as2&#038;o=1&#038;a=0201700735" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> at Amazon.</li>
</ul>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcppsource.com%2F2009%2F04%2Fclass-access-levels%2F&amp;linkname=Class%20Access%20Levels"><img src="http://cppsource.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://cppsource.com/2009/04/class-access-levels/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the assert macro in C++</title>
		<link>http://cppsource.com/2009/04/using-the-assert-macro-in-c/</link>
		<comments>http://cppsource.com/2009/04/using-the-assert-macro-in-c/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 03:44:53 +0000</pubDate>
		<dc:creator>carlos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cppsource.com/?p=27</guid>
		<description><![CDATA[Writing good software is a difficult proposition. It is sometimes easy to write sub-par software, with a high number of defects and poor maintainability, but to make sure that software has good quality and is easy to modify one needs to take care of many issues.
One of the necessary factors in creating good software is [...]]]></description>
			<content:encoded><![CDATA[<p>Writing good software is a difficult proposition. It is sometimes easy to write sub-par software, with a high number of defects and poor maintainability, but to make sure that software has good quality and is easy to modify one needs to take care of many issues.</p>
<p>One of the necessary factors in creating good software is guaranteeing that it satisfy its requirements. One of the simplest methods for doing this is using the assert facility provided by the standard C++ library.</p>
<h3>Using Assert</h3>
<p>Assert is a mechanism that allows a program to stop an error in its tracks whenever it is detected.  Assert is implemented in C++ as a macro, where the first argument is a boolean expression. This expression is evaluated, and its result is always expected to be true.</p>
<p>For example, the following is a typical use of assert:</p>
<p>void test(int value) {<br />
  assert(value &gt; 0);<br />
  // normal processing<br />
} </p>
<p>In the example above, the assert tests if the argument passed to function test is greater than zero. If this is true, processing continues as normal. However, in the case that value is negative or zero, the assertion fails and the program stops immediately.</p>
<p>The assert <a href="http://in4mationflow.com/Cplusplus/CLibFunctions/assert">macro is defined in assert.h</a>, a header file that is  also used by the C compiler &#8212; so you can use assert on C or C++.</p>
<p>Using assert in a testing environment is a first step towards guaranteeing that we have the correct values before our algorithm starts. It is an easy method and has been effectly used in this way by many experienced programmers. </p>
<h3>Disadvantages of Assert</h3>
<p>Although there are many advantages on using assert, one also needs to be aware of the problems in its use. First, asserts are not a substitute to using exceptions. When there is a possibility of recovery from an error, then the exception mechanism should be used. This way, the programmer has control over how a failure in the code must be handled. With an assert, on the other hand, the whole program stops.</p>
<p>In general, asserts should not be used on production code. The reason is that asserts are not graceful, they just stablish that an error ocurred, and stop the program. This is certainly not the behavior required in production, where the appropriate steps need to be taken to guarantee that user data is preserved.</p>
<h3>Conclusion</h3>
<p>Asserts are a powerful mechanism provided by C++ to guarantee that certain conditions are present on a specific function. If the expression given as input is not true, then the assert will automatically call the attention of the developer.</p>
<p>Asserts should be used sparingly, however, and not at all in production. Thankfully, all implementations of assert have a mechanism to disable it in production code. Check the documentation of NDEBUG in your favorite compiler for the details.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcppsource.com%2F2009%2F04%2Fusing-the-assert-macro-in-c%2F&amp;linkname=Using%20the%20assert%20macro%20in%20C%2B%2B"><img src="http://cppsource.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://cppsource.com/2009/04/using-the-assert-macro-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some features of the C++0x standard</title>
		<link>http://cppsource.com/2009/04/some-features-of-the-c0x-standard/</link>
		<comments>http://cppsource.com/2009/04/some-features-of-the-c0x-standard/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 04:25:26 +0000</pubDate>
		<dc:creator>carlos</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[standard]]></category>

		<guid isPermaLink="false">http://cppsource.com/?p=17</guid>
		<description><![CDATA[C++ is growing and changing. As has been the case for the last 20 years, C++ has been developing new features as a response to the changes in computing.
The latest trend of changes in the C++ language is due to the update of the Standard.
A work that has taken many years, the new standard that [...]]]></description>
			<content:encoded><![CDATA[<p>C++ is growing and changing. As has been the case for the last 20 years, C++ has been developing new features as a response to the changes in computing.<br />
The latest trend of changes in the C++ language is due to the update of the Standard.</p>
<p>A work that has taken many years, the new standard that is by now only known as C++0x (where x will be probably 9) will add many features that will change the way we develop software in C++.</p>
<h2>Improved Templates</h2>
<p>Templates are the feature in C++ that was used to create reusable containers for any object. Thanks to templates we have now a powerful standard library, with  containers (such as vectors, lists, trees, and strings), and algorithms that operate on these containers.</p>
<p>On the new standard, templates will be improved by providing information on the types that can be used to specialize a template. This is a form of type inference that will make templates much more useful (and with better error codes).</p>
<p>Another nice feature is the addition of auto detection of variable types. Since the type system of C++ is very sophisticated, it makes sense to have a feature in the language to allow types to be automatically detected. The language will use the keyword <strong>auto</strong> to determine that a variable has the same type as the right hand side on an assignment. So, something as</p>
<p>auto a = new MyObject(); </p>
<p>is sufficient to create a variable of the type MyObject. Notice that we just need to write MyObject once (while nowadays we needed to repeat the type name before the variable name).</p>
<h2>Other Features</h2>
<p>The new standard has also several provisions that will improve programming in many ways. Examples include new types of constants, improved initialization of objects, among others. I plan to write about some of these features in the future &#8212; stay tuned.</p>
<h2><strong>Related Articles</strong></h2>
<ul>
<li>Check my review of a book on the <a href="http://cppsource.com/2009/04/book-review-large-scale-c-software-design/">development of large-scale systems in C++</a>.</li>
<li>A discussion of <a href="http://in4mationflow.com/Cplusplus/Articles/Challenges-of-Large-Scale-C-Programming">issues in large scale C++ development</a>.</li>
</ul>
<div>
<a href="http://www.basecampHQ.com/?referrer=CARLOSOLIVEIRA"><img alt="Basecamp" border="0" height="125" src="https://affiliate.37signals.com/images/products/basecamp/banner-270x125.png" width="270" /></a>
</div>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcppsource.com%2F2009%2F04%2Fsome-features-of-the-c0x-standard%2F&amp;linkname=Some%20features%20of%20the%20C%2B%2B0x%20standard"><img src="http://cppsource.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://cppsource.com/2009/04/some-features-of-the-c0x-standard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Book review: Large-Scale C++ Software Design</title>
		<link>http://cppsource.com/2009/04/book-review-large-scale-c-software-design/</link>
		<comments>http://cppsource.com/2009/04/book-review-large-scale-c-software-design/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 01:22:30 +0000</pubDate>
		<dc:creator>carlos</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[compilation]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[large scale]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://cppsource.com/?p=8</guid>
		<description><![CDATA[The book &#8220;large-scale C++ software design&#8221; presents a clear explanation of the concepts necessary to understand large-scale C++ systems. When a project has thousands of files, it is very difficult to manage the complexity of changing code. By its very nature, C++ puts a lot of stress on the building system, and a project can [...]]]></description>
			<content:encoded><![CDATA[<p><iframe align="right" src="http://rcm.amazon.com/e/cm?t=bookdablogonb-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=0201633620&#038;md=10FE9736YVPPT7A0FBG2&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>The book &#8220;large-scale C++ software design&#8221; presents a clear explanation of the concepts necessary to understand large-scale C++ systems. When a project has thousands of files, it is very difficult to manage the complexity of changing code. By its very nature, C++ puts a lot of stress on the building system, and a project can just become extremely hard to maintain if the proper code dependencies are not taken care of.</p>
<p>In large scale C++, John Lakos provides a very thorough discussion of techniques to avoid dependencie problems in large projects. The author skips simple concepts that are commonly treated in other books, such as naming variables and other style issues. Instead, he concentrates in problems that happen only on large-scale systems.</p>
<p>Large-scale C++ was the first book to bring into discussion the issue of header files as a major source of dependencies between C++ modules. Due to the compilation model used in C++, where each file can potentially include hundreds of header files, changing one of these headers can have a large impact on other parts of your program. In fact, in C++ is not uncommon that a single change in a header files requires the recompilation of thousands of files, or even the whole project.</p>
<p>The book also discusses how to create modules that have as few dependencies as possible. This practice increases reuse of the resulting software, since a programmer doesn&#8217;t need to include hundreds of header files just to have access to a single functionality.</p>
<p>Large-scale C++ has hundreds of examples, and every concept is accompanied by sample code illustrating the concept. It is a very thorough book, and even though it is long, every page has some interesting idea.</p>
<p>You can check other reviews and information about <a href="http://www.amazon.com/gp/product/0201633620?ie=UTF8&#038;tag=bookdablogonb-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0201633620">Large-scale C++ software design</a> in its <a href="http://www.amazon.com/gp/product/0201633620?ie=UTF8&#038;tag=bookdablogonb-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0201633620">main web page</a>.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcppsource.com%2F2009%2F04%2Fbook-review-large-scale-c-software-design%2F&amp;linkname=Book%20review%3A%20Large-Scale%20C%2B%2B%20Software%20Design"><img src="http://cppsource.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://cppsource.com/2009/04/book-review-large-scale-c-software-design/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
