<?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>NerdyRoom™ &#187; c</title>
	<atom:link href="http://www.nrtm.de/index.php/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nrtm.de</link>
	<description>never read the manual</description>
	<lastBuildDate>Mon, 06 Feb 2012 21:02:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Programming Language Rant</title>
		<link>http://www.nrtm.de/index.php/2011/11/15/programming-language-rant/</link>
		<comments>http://www.nrtm.de/index.php/2011/11/15/programming-language-rant/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 10:49:46 +0000</pubDate>
		<dc:creator>robin</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[nerdcore]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[fortran]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.nrtm.de/?p=3735</guid>
		<description><![CDATA[From http://www.esm.psu.edu/~ajm138/fortranexamples.html: C programmers love to gloat that recursion can not be done in Fortran. When they say Fortran, they mean FORTRAN 77, since they absolutely refuse to acknowledge the existence of modern Fortran 90. (Fortran 90 came out over ten years ago. You would think they might know something about it by now!) But [...]]]></description>
			<content:encoded><![CDATA[<p>From <a href="http://www.esm.psu.edu/~ajm138/fortranexamples.html">http://www.esm.psu.edu/~ajm138/fortranexamples.html</a>:</p>
<blockquote><p>C programmers love to gloat that recursion can not be done in Fortran. When they say Fortran, they mean FORTRAN 77, since they absolutely refuse to acknowledge the existence of modern Fortran 90. (Fortran 90 came out over ten years ago. You would think they might know something about it by now!) But this example shows that even in FORTRAN 77 one can quickly and easily write routines that are recursive. So stick that in your complex variables, C programmers, and &#8212; oh wait, I forgot there are no complex variables in C. You have to manually define a complex data type. Not to mention having to write math functions like sin( ) and cos( ) to handle such data types.</p></blockquote>
<p>Hahaaa &#8230; love it!</p>
 <p><a href="http://www.nrtm.de/?flattrss_redirect&amp;id=3735&amp;md5=22737ae4948bb123a32c96f42e5b0f43" title="Flattr" target="_blank"><img src="https://www.nrtm.de/wp-content/plugins/flattrss/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.nrtm.de/index.php/2011/11/15/programming-language-rant/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<atom:link rel="payment" href="http://www.nrtm.de/?flattrss_redirect&amp;id=3735&amp;md5=22737ae4948bb123a32c96f42e5b0f43" type="text/html" />
	</item>
		<item>
		<title>Weihnachtsrätsel 3.12.</title>
		<link>http://www.nrtm.de/index.php/2009/12/03/weihnachtsratsel-3-12/</link>
		<comments>http://www.nrtm.de/index.php/2009/12/03/weihnachtsratsel-3-12/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 23:00:42 +0000</pubDate>
		<dc:creator>robin</dc:creator>
				<category><![CDATA[nerdcore]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[weihnachtsrätsel]]></category>

		<guid isPermaLink="false">http://www.nrtm.de/?p=2034</guid>
		<description><![CDATA[Welche numerische Funktion/Algorithmus realisiert folgende C Funktion? Wegen Schwierigkeiten mit dem Code, jetzt als Bild. Zum nrtm.de Weihnachtsrätsel.]]></description>
			<content:encoded><![CDATA[<p>Welche numerische Funktion/Algorithmus realisiert folgende C Funktion?</p>
<p>Wegen Schwierigkeiten mit dem Code, jetzt als Bild.</p>
<p><a href="http://www.nrtm.de/wp-content/uploads/2009/12/gl.png"><img class="aligncenter size-full wp-image-2058" title="gl" src="http://www.nrtm.de/wp-content/uploads/2009/12/gl.png" alt="gl" width="497" height="240" /></a></p>
<p><a href="http://www.nrtm.de/index.php/2009/12/01/nrtm-de-weihnachtsratsel/" target="_blank">Zum nrtm.de Weihnachtsrätsel.</a></p>
 <p><a href="http://www.nrtm.de/?flattrss_redirect&amp;id=2034&amp;md5=244f7b21b5271a0a258082c15cb6b38f" title="Flattr" target="_blank"><img src="https://www.nrtm.de/wp-content/plugins/flattrss/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.nrtm.de/index.php/2009/12/03/weihnachtsratsel-3-12/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.nrtm.de/?flattrss_redirect&amp;id=2034&amp;md5=244f7b21b5271a0a258082c15cb6b38f" type="text/html" />
	</item>
		<item>
		<title>external variables: C vs. C++</title>
		<link>http://www.nrtm.de/index.php/2009/07/07/external-variables-c-vs-c/</link>
		<comments>http://www.nrtm.de/index.php/2009/07/07/external-variables-c-vs-c/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 14:00:35 +0000</pubDate>
		<dc:creator>Willi</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[c]]></category>

		<guid isPermaLink="false">http://www.nrtm.de/?p=1383</guid>
		<description><![CDATA[The situation is the following: We have two source files (main and core) and want to use the same global variable (headvar) in both of them. In C thats plain easy: head.h #ifndef __HEAD_H__ #define __HEAD_H__ &#160; int headvar; /* global var */ &#160; int core&#40;int&#41;; /* forward declaration */ &#160; #endif /* __HEAD_H__ */ [...]]]></description>
			<content:encoded><![CDATA[<p>The situation is the following: We have two source files (main and core) and want to use the same global variable (headvar) in both of them. In C thats plain easy:</p>
<p>head.h</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifndef __HEAD_H__</span>
<span style="color: #339933;">#define __HEAD_H__</span>
&nbsp;
<span style="color: #993333;">int</span> headvar<span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* global var */</span>
&nbsp;
<span style="color: #993333;">int</span> core<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #808080; font-style: italic;">/* forward declaration */</span>
&nbsp;
<span style="color: #339933;">#endif /* __HEAD_H__ */</span></pre></div></div>

<p>main.c</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio .h&gt;</span>
<span style="color: #339933;">#include &quot;head.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;Hello World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;headvar: %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> headvar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;core: %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> core<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">23</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>stdio<span style="color: #339933;">&gt;</span></pre></div></div>

<p>core.c</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;head.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> core<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> var<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    headvar<span style="color: #339933;">=</span><span style="color: #0000dd;">42</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> res<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    res<span style="color: #339933;">=</span>var<span style="color: #339933;">+</span>headvar<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> res<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In C++ this isn&#8217;t going to work as the variable is basically declared twice using the mechanisms as seen above. The reasons for that are quiet complex &#8211; you can gain more insight here: <a href="http://www.hardforum.com/showthread.php?t=1184351">http://www.hardforum.com</a>. I&#8217;ll only talk about the solution here. You have to make sure that there is only one single memory allocation aka instance of the variable. To ensure this you can use the keyword &#8220;external&#8221; to tell the compiler-suite that the actual definition occurs elsewhere. Using external you basically tell that there is a name refering to a variable of the given type without actual declaration. You can use this external variable in multiple locations as long as there is one single declaration in one location. So here is the sourcecode of above recoded in C++.</p>
<p>head.h</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifndef __HEAD_H__</span>
<span style="color: #339933;">#define __HEAD_H__</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">int</span> newvar<span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* external declaration */</span>
&nbsp;
<span style="color: #993333;">int</span> core<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #808080; font-style: italic;">/* forward declaration */</span>
&nbsp;
<span style="color: #339933;">#endif /* __HEAD_H__ */</span></pre></div></div>

<p>main.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;iostream&gt;</span>
<span style="color: #339933;">#include &quot;head.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> newvar <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>	<span style="color: #808080; font-style: italic;">/* actual definition */</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	std<span style="color: #339933;">::</span><span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;</span> <span style="color: #339933;">&lt;</span> <span style="color: #ff0000;">&quot;Hello World!&quot;</span> <span style="color: #339933;">&lt;&lt;</span> std<span style="color: #339933;">::</span><span style="color: #202020;">endl</span><span style="color: #339933;">;</span>
	std<span style="color: #339933;">::</span><span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;newvar: &quot;</span> <span style="color: #339933;">&lt;&lt;</span> newvar <span style="color: #339933;">&lt;&lt;</span> std<span style="color: #339933;">::</span><span style="color: #202020;">endl</span><span style="color: #339933;">;</span>
	std<span style="color: #339933;">::</span><span style="color: #000066;">cout</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #ff0000;">&quot;core: &quot;</span> <span style="color: #339933;">&lt;&lt;</span> core<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">23</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> std<span style="color: #339933;">::</span><span style="color: #202020;">endl</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>core.cpp</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;head.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> core<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> var<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> res<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    newvar<span style="color: #339933;">=</span><span style="color: #0000dd;">42</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* new value for variable defined in main.cpp */</span>
    res<span style="color: #339933;">=</span>var<span style="color: #339933;">+</span>newvar<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> res<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is nothing new for the casual programmer but can be a real pain in the ass when the error has already been made as the error-messages are mostly &#8220;slightly&#8221; irritating.</iostream></pre>
 <p><a href="http://www.nrtm.de/?flattrss_redirect&amp;id=1383&amp;md5=8322a86b6b5e2157a8f5796abf2a96f7" title="Flattr" target="_blank"><img src="https://www.nrtm.de/wp-content/plugins/flattrss/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.nrtm.de/index.php/2009/07/07/external-variables-c-vs-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.nrtm.de/?flattrss_redirect&amp;id=1383&amp;md5=8322a86b6b5e2157a8f5796abf2a96f7" type="text/html" />
	</item>
		<item>
		<title>Displaying Enum-Names instead of Values in Errors</title>
		<link>http://www.nrtm.de/index.php/2009/06/04/displaying-enum-names-instead-of-values-in-errors/</link>
		<comments>http://www.nrtm.de/index.php/2009/06/04/displaying-enum-names-instead-of-values-in-errors/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 10:08:54 +0000</pubDate>
		<dc:creator>ronny</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[qt]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[convenience]]></category>
		<category><![CDATA[enum]]></category>
		<category><![CDATA[error]]></category>

		<guid isPermaLink="false">http://www.nrtm.de/?p=1183</guid>
		<description><![CDATA[The usual lazy developer likes to print out plain error messages, which contain pure numbers and don&#8217;t mean anything. The most prominent example is Windows itself, back in the old days. Some time ago I was once again faced with the decision how to handle errors in our plug-able class design. While trying to find [...]]]></description>
			<content:encoded><![CDATA[<p>The usual lazy developer likes to print out plain error messages, which contain pure numbers and don&#8217;t mean anything. The most prominent example is Windows itself, back in the old days.</p>
<p>Some time ago I was once again faced with the decision how to handle errors in our plug-able class design. While trying to find a good solution I remembered an old <a href="http://labs.trolltech.com/blogs/2008/10/09/coding-tip-pretty-printing-enum-values/">blog post</a> &#8211; Qt is able to create a mapping from enum integer values to their respective names. So I fiddled around a bit and came up with a pretty neat solution, which requires virtually no extra code to gain a little more meaningful error messages than plain integer values.</p>
<p>Here is what I came up with:</p>
<p>You need to sub-class an error base class, insert your error enumeration, and tell Qt that you need the value-to-name mapping for this enum. All the extras come with the base class.<br />
If you don&#8217;t like the rest of the class, you might just want to check out BaseError::identifierToName(). That&#8217;s where the magic is happening.</p>
<h2>A usage example:</h2>

<div class="wp_syntax"><div class="code"><pre class="cpp-qt" style="font-family:monospace;"> <span style="color: #888888;">// --- header file ---</span>
 <span style="color: #0057AE;">class</span> HorseError <span style="color: #006E28;">:</span> <span style="color: #0057AE;">public</span> BaseError
 <span style="color: #006E28;">&#123;</span>
     <span style="color: #0057AE;">Q_OBJECT</span>
     Q_ENUMS<span style="color: #006E28;">&#40;</span>x<span style="color: #006E28;">&#41;</span>
 <span style="color: #0057AE;">public</span><span style="color: #006E28;">:</span>
     <span style="color: #0057AE;">enum</span> x <span style="color: #006E28;">&#123;</span> DOES_NOT_WANT<span style="color: #006E28;">,</span> IS_INJURED <span style="color: #006E28;">&#125;</span><span style="color: #006E28;">;</span>
 <span style="color: #006E28;">&#125;</span><span style="color: #006E28;">;</span>
&nbsp;
 <span style="color: #0057AE;">class</span> Horse
 <span style="color: #006E28;">&#123;</span>
 <span style="color: #0057AE;">public</span><span style="color: #006E28;">:</span>
     HorseError error<span style="color: #006E28;">;</span>
&nbsp;
     <span style="color: #0057AE;">void</span> ride<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
 <span style="color: #006E28;">&#125;</span><span style="color: #006E28;">;</span>
&nbsp;
 <span style="color: #888888;">// --- source file ---</span>
 <span style="color: #0057AE;">void</span> Horse<span style="color: #006E28;">::</span><span style="color: #2B74C7;">ride</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span>
 <span style="color: #006E28;">&#123;</span>
     error.<span style="color: #2B74C7;">activate</span><span style="color: #006E28;">&#40;</span>HorseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">DOES_NOT_WANT</span><span style="color: #006E28;">,</span> <span style="color: #BF0303;">&quot;cannot happen anyways&quot;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
 <span style="color: #006E28;">&#125;</span>
&nbsp;
 <span style="color: #888888;">// --- using ---</span>
&nbsp;
 Horse mybeautifulhorse<span style="color: #006E28;">;</span>
 mybeautifulhorse.<span style="color: #2B74C7;">ride</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span>
&nbsp;
 <span style="color: #888888;">// operator() returns a bool</span>
 <span style="color: #000000; font-weight:bold;">if</span> <span style="color: #006E28;">&#40;</span>mybeautifulhorse.<span style="color: #2B74C7;">error</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&#123;</span>
    <span style="color: #888888;">// operator QString makes the error readable</span>
    <span style="color: #0057AE;">qDebug</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&lt;</span> <span style="color: #006E28;">&lt;</span> mybeautifulhorse.<span style="color: #2B74C7;">error</span><span style="color: #006E28;">;</span>
&nbsp;
    <span style="color: #2B74C7;">exit</span><span style="color: #006E28;">&#40;</span><span style="color: #B08000;">1</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
 <span style="color: #006E28;">&#125;</span></pre></div></div>

<h2>The Header File &#8211; BaseError.hpp</h2>
</pre>

<div class="wp_syntax"><div class="code"><pre class="cpp-qt" style="font-family:monospace;"><span style="color: #006E28;">#ifndef BASE_ERROR_HPP</span>
<span style="color: #006E28;">#define BASE_ERROR_HPP</span>
&nbsp;
<span style="color: #0057AE;">class</span> BaseError <span style="color: #006E28;">:</span> <span style="color: #0057AE;">private</span> <span style="color: #22aadd;">QObject</span>                                                                                                         
<span style="color: #006E28;">&#123;</span>                                                                                                                                         
    <span style="color: #0057AE;">Q_OBJECT</span>
<span style="color: #0057AE;">public</span><span style="color: #006E28;">:</span>
&nbsp;
    BaseError<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
    <span style="color: #0057AE;">virtual</span> ~BaseError<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
&nbsp;
    <span style="color: #888888; font-style: italic;">/** @returns whether the error is active */</span>
    <span style="color: #0057AE;">bool</span> active<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> const<span style="color: #006E28;">;</span>                       
    <span style="color: #888888; font-style: italic;">/** @returns the identifier (an enum) */</span>   
    <span style="color: #0057AE;">int</span> identifier<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> const<span style="color: #006E28;">;</span>                    
    <span style="color: #888888; font-style: italic;">/** @returns the name of the identifier enum e.g. &quot;MyErrorClass::MyReturnValue&quot; */</span>
    <span style="color: #22aadd;">QString</span> name<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> const<span style="color: #006E28;">;</span>                                                             
    <span style="color: #888888; font-style: italic;">/** @returns the description given by activate's second parameter */</span>              
    <span style="color: #0057AE;">const</span> <span style="color: #22aadd;">QString</span><span style="color: #006E28;">&amp;</span> description<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> const<span style="color: #006E28;">;</span>                                               
&nbsp;
    <span style="color: #888888; font-style: italic;">/** throw the error */</span>
    <span style="color: #0057AE;">void</span> activate<span style="color: #006E28;">&#40;</span><span style="color: #0057AE;">int</span> identifier<span style="color: #006E28;">,</span> <span style="color: #0057AE;">const</span> <span style="color: #22aadd;">QString</span><span style="color: #006E28;">&amp;</span> description <span style="color: #006E28;">=</span> <span style="color: #22aadd;">QString</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
    <span style="color: #888888; font-style: italic;">/** clear the state / remove the error */</span>                             
    <span style="color: #0057AE;">void</span> deactivate<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>                                                    
&nbsp;
&nbsp;
&nbsp;
    <span style="color: #888888; font-style: italic;">/** alias for active() */</span>
    <span style="color: #0057AE;">bool</span> operator<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> const<span style="color: #006E28;">;</span> 
&nbsp;
    <span style="color: #888888; font-style: italic;">/** @returns 'name() &quot;description()&quot;' e.g.
     * 'MyErrorClass::MyReturnValue &quot;something meaningful&quot;'
     */</span>                                                    
    operator <span style="color: #22aadd;">QString</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> const<span style="color: #006E28;">;</span>                              
&nbsp;
<span style="color: #0057AE;">signals</span><span style="color: #006E28;">:</span>
&nbsp;
    <span style="color: #888888; font-style: italic;">/** emitted when the error has been activated */</span>
    <span style="color: #0057AE;">void</span> activated<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>                               
&nbsp;
<span style="color: #0057AE;">protected</span><span style="color: #006E28;">:</span>
&nbsp;
    <span style="color: #888888; font-style: italic;">/**
     * Default implementation generates a string using the
     * &quot;DerivedErrorClassName::EnumeratorValueName&quot;       
     *                                                    
     * @note                                              
     * reimplement this functions to apply an own         
     * identifier-to-error-name-mapping                   
     */</span>                                                   
    <span style="color: #0057AE;">virtual</span> <span style="color: #22aadd;">QString</span> identifierToName<span style="color: #006E28;">&#40;</span><span style="color: #0057AE;">int</span> identifier<span style="color: #006E28;">&#41;</span> const<span style="color: #006E28;">;</span>
&nbsp;
<span style="color: #0057AE;">private</span><span style="color: #006E28;">:</span>
&nbsp;
    <span style="color: #0057AE;">bool</span> m_active<span style="color: #006E28;">;</span>
    <span style="color: #0057AE;">int</span> m_identifier<span style="color: #006E28;">;</span>
    <span style="color: #22aadd;">QString</span> m_description<span style="color: #006E28;">;</span>
<span style="color: #006E28;">&#125;</span><span style="color: #006E28;">;</span>
&nbsp;
&nbsp;
<span style="color: #006E28;">#endif</span></pre></div></div>

<h2>The Source File - BaseError.cpp</h2>

<div class="wp_syntax"><div class="code"><pre class="cpp-qt" style="font-family:monospace;"><span style="color: #006E28;">#include &quot;BaseError.hpp&quot;</span>
<span style="color: #006E28;">#include &lt;qmetaenum&gt;</span>
&nbsp;
BaseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">BaseError</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">:</span> <span style="color: #22aadd;">QObject</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">,</span> m_active<span style="color: #006E28;">&#40;</span><span style="color: #0057AE;">false</span><span style="color: #006E28;">&#41;</span>
<span style="color: #006E28;">&#123;</span>                                                  
<span style="color: #006E28;">&#125;</span>                                                  
&nbsp;
&nbsp;
BaseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">~BaseError</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span>
<span style="color: #006E28;">&#123;</span>                      
<span style="color: #006E28;">&#125;</span>                      
&nbsp;
&nbsp;
<span style="color: #0057AE;">bool</span> BaseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">operator</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #0057AE;">const</span>
<span style="color: #006E28;">&#123;</span>                                 
    <span style="color: #000000; font-weight:bold;">return</span> m_active<span style="color: #006E28;">;</span>              
<span style="color: #006E28;">&#125;</span>                                 
&nbsp;
&nbsp;
BaseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">operator</span> <span style="color: #22aadd;">QString</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #0057AE;">const</span>
<span style="color: #006E28;">&#123;</span>                                  
    <span style="color: #000000; font-weight:bold;">return</span> <span style="color: #22aadd;">QString</span><span style="color: #006E28;">&#40;</span><span style="color: #BF0303;">&quot;%1 <span style="color: #000099; font-weight: bold;">\&quot;</span>%2<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #006E28;">&#41;</span>.<span style="color: #2B74C7;">arg</span><span style="color: #006E28;">&#40;</span>name<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span>.<span style="color: #2B74C7;">arg</span><span style="color: #006E28;">&#40;</span>description<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
<span style="color: #006E28;">&#125;</span>                                                              
&nbsp;
&nbsp;
<span style="color: #0057AE;">int</span> BaseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">identifier</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #0057AE;">const</span>
<span style="color: #006E28;">&#123;</span>                                
    <span style="color: #000000; font-weight:bold;">return</span> m_identifier<span style="color: #006E28;">;</span>         
<span style="color: #006E28;">&#125;</span>                                
&nbsp;
&nbsp;
<span style="color: #22aadd;">QString</span> BaseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">name</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #0057AE;">const</span>
<span style="color: #006E28;">&#123;</span>
    <span style="color: #000000; font-weight:bold;">return</span> identifierToName<span style="color: #006E28;">&#40;</span>m_identifier<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
<span style="color: #006E28;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0057AE;">const</span> <span style="color: #22aadd;">QString</span><span style="color: #006E28;">&amp;</span> BaseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">description</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span> <span style="color: #0057AE;">const</span>
<span style="color: #006E28;">&#123;</span>
    <span style="color: #000000; font-weight:bold;">return</span> m_description<span style="color: #006E28;">;</span>
<span style="color: #006E28;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0057AE;">void</span> BaseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">activate</span><span style="color: #006E28;">&#40;</span><span style="color: #0057AE;">int</span> identifier<span style="color: #006E28;">,</span> <span style="color: #0057AE;">const</span> <span style="color: #22aadd;">QString</span><span style="color: #006E28;">&amp;</span> description<span style="color: #006E28;">&#41;</span>
<span style="color: #006E28;">&#123;</span>
    m_identifier <span style="color: #006E28;">=</span> identifier<span style="color: #006E28;">;</span>
    m_description <span style="color: #006E28;">=</span> description<span style="color: #006E28;">;</span>
    m_active <span style="color: #006E28;">=</span> true<span style="color: #006E28;">;</span>
&nbsp;
    emit activated<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
<span style="color: #006E28;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0057AE;">void</span> BaseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">deactivate</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span>
<span style="color: #006E28;">&#123;</span>
    m_active <span style="color: #006E28;">=</span> false<span style="color: #006E28;">;</span>
<span style="color: #006E28;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #22aadd;">QString</span> BaseError<span style="color: #006E28;">::</span><span style="color: #2B74C7;">identifierToName</span><span style="color: #006E28;">&#40;</span><span style="color: #0057AE;">int</span> identifier<span style="color: #006E28;">&#41;</span> <span style="color: #0057AE;">const</span>
<span style="color: #006E28;">&#123;</span>
    <span style="color: #22aadd;">QStringList</span> ret<span style="color: #006E28;">;</span>
&nbsp;
    <span style="color: #0057AE;">const</span> <span style="color: #22aadd;">QMetaObject</span><span style="color: #006E28;">*</span> meta <span style="color: #006E28;">=</span> metaObject<span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
&nbsp;
    <span style="color: #000000; font-weight:bold;">for</span> <span style="color: #006E28;">&#40;</span><span style="color: #0057AE;">int</span> i<span style="color: #006E28;">=</span><span style="color: #B08000;">0</span><span style="color: #006E28;">;</span> i <span style="color: #006E28;">&lt;</span> meta<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">enumeratorCount</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span> <span style="color: #006E28;">++</span>i<span style="color: #006E28;">&#41;</span> <span style="color: #006E28;">&#123;</span>
&nbsp;
        <span style="color: #22aadd;">QMetaEnum</span> enumerator <span style="color: #006E28;">=</span> meta<span style="color: #006E28;">-&gt;</span><span style="color: #2B74C7;">enumerator</span><span style="color: #006E28;">&#40;</span>i<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
&nbsp;
        enumerator.<span style="color: #2B74C7;">key</span><span style="color: #006E28;">&#40;</span>identifier<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
&nbsp;
        ret <span style="color: #006E28;">&lt;</span> <span style="color: #006E28;">&lt;</span> <span style="color: #22aadd;">QString</span><span style="color: #006E28;">&#40;</span><span style="color: #BF0303;">&quot;%1::%2&quot;</span><span style="color: #006E28;">&#41;</span>.<span style="color: #2B74C7;">arg</span><span style="color: #006E28;">&#40;</span>enumerator.<span style="color: #2B74C7;">scope</span><span style="color: #006E28;">&#40;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span>.<span style="color: #2B74C7;">arg</span><span style="color: #006E28;">&#40;</span>enumerator.<span style="color: #2B74C7;">key</span><span style="color: #006E28;">&#40;</span>identifier<span style="color: #006E28;">&#41;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
    <span style="color: #006E28;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight:bold;">return</span> ret.<span style="color: #2B74C7;">join</span><span style="color: #006E28;">&#40;</span><span style="color: #BF0303;">&quot;, &quot;</span><span style="color: #006E28;">&#41;</span><span style="color: #006E28;">;</span>
<span style="color: #006E28;">&#125;</span></pre></div></div>

<p></qmetaenum></pre>
<p><meta name="author" content="Ronny Brendel" /><br />
<meta name="keywords" contenxt="C++,Qt,programming,convenience,debug, debugging,enum,error" /></p>
 <p><a href="http://www.nrtm.de/?flattrss_redirect&amp;id=1183&amp;md5=49a2c13cacbb7f464b7e075f30d99c24" title="Flattr" target="_blank"><img src="https://www.nrtm.de/wp-content/plugins/flattrss/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.nrtm.de/index.php/2009/06/04/displaying-enum-names-instead-of-values-in-errors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="http://www.nrtm.de/?flattrss_redirect&amp;id=1183&amp;md5=49a2c13cacbb7f464b7e075f30d99c24" type="text/html" />
	</item>
		<item>
		<title>source code comments update</title>
		<link>http://www.nrtm.de/index.php/2009/04/26/source-code-comments-update/</link>
		<comments>http://www.nrtm.de/index.php/2009/04/26/source-code-comments-update/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 12:02:28 +0000</pubDate>
		<dc:creator>Willi</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[nerdcore]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[Quake3]]></category>

		<guid isPermaLink="false">http://www.nrtm.de/?p=784</guid>
		<description><![CDATA[One of the posts in yesterdays article (source-code-comments) was about a function used in the Quake3 sources. I fiddled around with it to understand what it does and am amazed of the compute power of this little snippet: #define _GNU_SOURCE #include &#60;assert .h&#62; #include &#60;stdio .h&#62; #include &#60;stdlib .h&#62; #include &#60;math .h&#62; &#160; float Q_rsqrt&#40; [...]]]></description>
			<content:encoded><![CDATA[<p>One of the posts in yesterdays article (<a href="http://www.nrtm.de/index.php/2009/04/25/source-code-comments/">source-code-comments</a>) was about a function used in the Quake3 sources. I fiddled around with it to understand what it does and am amazed of the compute power of this little snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define _GNU_SOURCE</span>
<span style="color: #339933;">#include &lt;assert .h&gt;</span>
<span style="color: #339933;">#include &lt;stdio .h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib .h&gt;</span>
<span style="color: #339933;">#include &lt;math .h&gt;</span>
&nbsp;
<span style="color: #993333;">float</span> Q_rsqrt<span style="color: #009900;">&#40;</span> <span style="color: #993333;">float</span> number <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">long</span> i<span style="color: #339933;">;</span>
  <span style="color: #993333;">float</span> x2<span style="color: #339933;">,</span> y<span style="color: #339933;">;</span>
  <span style="color: #993333;">const</span> <span style="color: #993333;">float</span> threehalfs <span style="color: #339933;">=</span> <span style="color:#800080;">1.5F</span><span style="color: #339933;">;</span>
&nbsp;
  x2 <span style="color: #339933;">=</span> number <span style="color: #339933;">*</span> <span style="color:#800080;">0.5F</span><span style="color: #339933;">;</span>
  y  <span style="color: #339933;">=</span> number<span style="color: #339933;">;</span>
  i  <span style="color: #339933;">=</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>y<span style="color: #339933;">;</span>  <span style="color: #808080; font-style: italic;">/* evil floating point bit level hacking */</span>
  i  <span style="color: #339933;">=</span> <span style="color: #208080;">0x5f3759df</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span> i <span style="color: #339933;">&gt;&gt;</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* what the fuck? */</span>
  y  <span style="color: #339933;">=</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span> <span style="color: #993333;">float</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>i<span style="color: #339933;">;</span>
  y  <span style="color: #339933;">=</span> y <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span> threehalfs <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span> x2 <span style="color: #339933;">*</span> y <span style="color: #339933;">*</span> y <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* 1st iteration */</span>
  <span style="color: #808080; font-style: italic;">/* y  = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed */</span>
&nbsp;
  <span style="color: #339933;">#ifndef Q3_VM</span>
  <span style="color: #339933;">#ifdef __linux__</span>
    assert<span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>isnan<span style="color: #009900;">&#40;</span>y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* bk010122 - FPE? */</span>
  <span style="color: #339933;">#endif</span>
  <span style="color: #339933;">#endif</span>
  <span style="color: #b1b100;">return</span> y<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> 
main <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span> line <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    size_t len <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">float</span> input<span style="color: #339933;">=</span><span style="color:#800080;">42.23</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;type in a float e.g: 42.23<span style="color: #000099; font-weight: bold;">\n</span>value: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    getline<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>line<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>len<span style="color: #339933;">,</span> stdin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    input <span style="color: #339933;">=</span> strtof<span style="color: #009900;">&#40;</span>line<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">**</span><span style="color: #009900;">&#41;</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;reverse square root of %.6f is %.6f <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> input<span style="color: #339933;">,</span> Q_rsqrt<span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span> free<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>math<span style="color: #339933;">&gt;&lt;/</span>stdlib<span style="color: #339933;">&gt;&lt;/</span>stdio<span style="color: #339933;">&gt;&lt;/</span>assert<span style="color: #339933;">&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> <span style="color: #c20cb9; font-weight: bold;">gcc</span> <span style="color: #660033;">-ansi</span> <span style="color: #660033;">-pedantic</span> <span style="color: #660033;">-Wall</span> -D_XOPEN_SOURCE=<span style="color: #000000;">600</span> Q3_inv_sqrt.c <span style="color: #660033;">-o</span> Q3rsqrt</pre></div></div>

 <p><a href="http://www.nrtm.de/?flattrss_redirect&amp;id=784&amp;md5=0cd8a1147c481db981a7b2f95a3882e3" title="Flattr" target="_blank"><img src="https://www.nrtm.de/wp-content/plugins/flattrss/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.nrtm.de/index.php/2009/04/26/source-code-comments-update/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="http://www.nrtm.de/?flattrss_redirect&amp;id=784&amp;md5=0cd8a1147c481db981a7b2f95a3882e3" type="text/html" />
	</item>
		<item>
		<title>Böser Code: member pointer in C++</title>
		<link>http://www.nrtm.de/index.php/2009/04/14/boser-code-member-pointer-in-c/</link>
		<comments>http://www.nrtm.de/index.php/2009/04/14/boser-code-member-pointer-in-c/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 21:47:10 +0000</pubDate>
		<dc:creator>stefan</dc:creator>
				<category><![CDATA[software]]></category>
		<category><![CDATA[böser code]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[member]]></category>
		<category><![CDATA[pointer]]></category>

		<guid isPermaLink="false">http://www.nrtm.de/?p=617</guid>
		<description><![CDATA[Vor einiger Zeit in ich auf etwas aufmerksam geworden, das mir erstauntes Kopfschütteln entlockt hat &#8211; member pointer. Aber vorher etwas C++-Theorie Das Klassenkonstrukt in C++ kennt u.a. die Schlüsselwörter public und private, die dazu dienen den Zugriff auf so markierten Member der Klasse zu beschränken. public steht dabei für allgemeinen Lese- und Schreibzugriff, privat [...]]]></description>
			<content:encoded><![CDATA[<p>Vor einiger Zeit in ich auf etwas aufmerksam geworden, das mir erstauntes Kopfschütteln entlockt hat &#8211; <em>member pointer</em>.<br />
Aber vorher etwas C++-Theorie <img src='http://www.nrtm.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Das Klassenkonstrukt in C++ kennt u.a. die Schlüsselwörter <strong>public</strong> und <strong>private</strong>, die dazu dienen den Zugriff auf so markierten Member der Klasse zu beschränken. public steht dabei für allgemeinen Lese- und Schreibzugriff, privat beschränkt den Zugriff auf Memberfunktionen der Klasse. Dazu ein kurzes Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> someClass<span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
      <span style="color: #0000ff;">int</span> one<span style="color: #008080;">;</span>        <span style="color: #666666;">// öffentliche Variable</span>
      <span style="color: #0000ff;">void</span> f<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
      <span style="color: #0000ff;">int</span> two<span style="color: #008080;">;</span>        <span style="color: #666666;">// private Variable</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
...
<span style="color: #007788;">someClass</span> Instance<span style="color: #008080;">;</span>
...
<span style="color: #007788;">Instance</span>.<span style="color: #007788;">one</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>        <span style="color: #666666;">// erlaubt</span>
&nbsp;
Instance.<span style="color: #007788;">two</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>        <span style="color: #666666;">// verboten - compiler error</span></pre></div></div>

<p>Member Pointer sind eine spezielle Form von Zeigern, die nur auf Member von Instanzen von der selben Klasse zeigen dürfen. Realisiert wird das durch relative Addressierung im Speicherbereich der Klasseninstanz. Für obriges Beispiel sieht das zum Beispiel so aus.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// pMember ist global und zeigt auf 'one' einer beliebigen Instanz von 'someClass'</span>
<span style="color: #0000ff;">int</span> someClass<span style="color: #008080;">::</span><span style="color: #000040;">*</span>pMember <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>someClass<span style="color: #008080;">::</span><span style="color: #007788;">one</span><span style="color: #008080;">;</span>
...
<span style="color: #007788;">Instance</span>.<span style="color: #000040;">*</span>pMember <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>     <span style="color: #666666;">// erlaubt, da 'one' public ist</span>
...
<span style="color: #007788;">pMember</span> <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>someClass<span style="color: #008080;">::</span><span style="color: #007788;">two</span><span style="color: #008080;">;</span>     <span style="color: #666666;">//    compiler error, da</span>
Instance.<span style="color: #000040;">*</span>pMember <span style="color: #000080;">=</span> <span style="color: #0000dd;">23</span><span style="color: #008080;">;</span>            <span style="color: #666666;">//     'two' private ist</span></pre></div></div>

<p>Hübsche Syntax oder? <img src='http://www.nrtm.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Nebenbei ist &#8216;<strong>.*</strong>&#8216; keine Kombination aus Punkt- und Dereferenzierungsoperator, sondern stellt einen eigenständigen Operator für member pointer dar.<br />
Bis hierhin ist das noch absolut stressfrei, da private greift und den Zugriff beschränkt. Anders sieht das allerdings aus, wenn der Zeiger seinen Inhalt innerhalb der Klasse bekommt. Hier kommt die Member-Funktion &#8216;<strong>f()</strong>&#8216; ins Spiel.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> someClass<span style="color: #008080;">::</span><span style="color: #007788;">f</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   pMember <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>someClass<span style="color: #008080;">::</span><span style="color: #007788;">two</span><span style="color: #008080;">;</span>        <span style="color: #666666;">// erlaubt, da innerhalb der Klasse!</span>
<span style="color: #008000;">&#125;</span>
...
<span style="color: #007788;">Instance</span>.<span style="color: #007788;">f</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>                    <span style="color: #666666;">// Sesam öffne dich:</span>
                                 <span style="color: #666666;">// ab hier hat private seine Bedeutung verloren !</span>
&nbsp;
Instance.<span style="color: #000040;">*</span>pMember <span style="color: #000080;">=</span> <span style="color: #0000dd;">42</span><span style="color: #008080;">;</span>        <span style="color: #666666;">// erlaubt aber BÖSE :)</span></pre></div></div>

<p>In der Praxis kommen Zeiger auf Member-Variablen recht selten vor. Pointer auf Member-Funktionen sind damit aber auch möglich und wohl sinnvoller. Zum Abschluss lass ich die Syntax nochmal für sich selbst sprechen.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> <span style="color: #008000;">&#40;</span>someClass<span style="color: #008080;">::</span><span style="color: #000040;">*</span>pFunc<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>someClass<span style="color: #008080;">::</span><span style="color: #007788;">f</span><span style="color: #008080;">;</span>    <span style="color: #666666;">// Deklaration</span>
...
<span style="color: #008000;">&#40;</span>someInstance.<span style="color: #000040;">*</span>pFunc<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>                            <span style="color: #666666;">// Aufruf</span></pre></div></div>

 <p><a href="http://www.nrtm.de/?flattrss_redirect&amp;id=617&amp;md5=41deec939e36186af490f2835a662d13" title="Flattr" target="_blank"><img src="https://www.nrtm.de/wp-content/plugins/flattrss/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.nrtm.de/index.php/2009/04/14/boser-code-member-pointer-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.nrtm.de/?flattrss_redirect&amp;id=617&amp;md5=41deec939e36186af490f2835a662d13" type="text/html" />
	</item>
		<item>
		<title>C/C++ Pointer-Arithmetics</title>
		<link>http://www.nrtm.de/index.php/2009/02/17/cc-pointer-arithmetic/</link>
		<comments>http://www.nrtm.de/index.php/2009/02/17/cc-pointer-arithmetic/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 14:16:46 +0000</pubDate>
		<dc:creator>jupp</dc:creator>
				<category><![CDATA[software]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[pointer]]></category>

		<guid isPermaLink="false">http://www.nrtm.de/?p=78</guid>
		<description><![CDATA[I recently found this when looking up how C/C++ organizes memory when allocating arrays, specifically multi-dimensional arrays. You can access the array #define X 5 #define Y 10 int data[X][Y]; either using data[x][y] or *(*(data + x) + y)]]></description>
			<content:encoded><![CDATA[<p>I recently found <a href="http://home.netcom.com/~tjensen/ptr/ch7x.htm" target="_blank">this</a> when looking up how C/C++ organizes memory when allocating arrays, specifically multi-dimensional arrays.</p>
<p>You can access the array<br />
<code><br />
    #define X 5<br />
    #define Y 10<br />
    int data[X][Y];<br />
</code></p>
<p>either using<br />
<code><br />
    data[x][y]<br />
</code><br />
or<br />
<code><br />
*(*(data + x) + y)<br />
</code></p>
 <p><a href="http://www.nrtm.de/?flattrss_redirect&amp;id=78&amp;md5=982a2c8c148e6402238d8de8d79482a9" title="Flattr" target="_blank"><img src="https://www.nrtm.de/wp-content/plugins/flattrss/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.nrtm.de/index.php/2009/02/17/cc-pointer-arithmetic/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<atom:link rel="payment" href="http://www.nrtm.de/?flattrss_redirect&amp;id=78&amp;md5=982a2c8c148e6402238d8de8d79482a9" type="text/html" />
	</item>
	</channel>
</rss>

