<?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>Visual C++ Tips &#187; C++</title>
	<atom:link href="http://weseetips.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://weseetips.com</link>
	<description>Gold mine of Visual C++ tricks!</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>How to Tokenize String? Different Tricks in Trade!</title>
		<link>http://weseetips.com/2010/02/11/how-to-tokenize-string-different-tricks-in-trade/</link>
		<comments>http://weseetips.com/2010/02/11/how-to-tokenize-string-different-tricks-in-trade/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 18:07:12 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Visual C++]]></category>
		<category><![CDATA[C++ string tokenize]]></category>
		<category><![CDATA[CStringT::Tokenize()]]></category>
		<category><![CDATA[istringstream]]></category>
		<category><![CDATA[split string]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[stringstream]]></category>
		<category><![CDATA[strtok()]]></category>
		<category><![CDATA[tokenize]]></category>
		<category><![CDATA[tokenize string]]></category>

		<guid isPermaLink="false">http://weseetips.com/?p=1350</guid>
		<description><![CDATA[I still remember my first project which I did seven years back. In that I got a task to split a string which contains ids separated by slash. I wrote a big snippet of string parser code by using pointers. But now when i see the following tricks, i feel &#8211; how childish was my [...]]]></description>
			<content:encoded><![CDATA[<p>I still remember my first project which I did seven years back. In that I got a task to split a string which contains ids separated by slash. I wrote a big snippet of string parser code by using pointers. But now when i see the following tricks, i feel &#8211; how childish was my first code snippet.</p>
<p><img class="alignnone size-full wp-image-1355" title="StrTokenize" src="http://weseetips.com/wp-content/uploads/2010/02/StrTokenize.jpg" alt="" width="540" height="383" /></p>
<p><img class="alignnone size-full wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.com/wp-content/uploads/2008/03/icon_howcanidoit.jpg" alt="" width="220" height="32" /><br />
Some of the tricks are as follows.</p>
<p><span style="text-decoration: underline;"><strong>1) strtok()</strong></span></p>
<p>If you want plain api and no object oriented fanciness, then strtok is for you. Download the source from <a href="http://weseetips.com/wp-content/uploads/2010/02/StrTok.zip">here</a>.</p>
<pre>#include "string.h"
...

// String to be splitted.
char String[] = "Long.Live_Visual.C++";

// Seperators.
char Seperators[] = "._";

// Start Tokenizing.
char* Token = strtok(String, Seperators);

// Loop until end.
while(Token != NULL)
{
 cout &lt;&lt; Token &lt;&lt; endl;

 // Tokenize the remaning string.
 Token = strtok(0, Seperators);
};
</pre>
<p><span style="text-decoration: underline;"><strong>2) istringstream</strong></span><br />
C++ Streams are good option for string splitup. But it has only one drawback &#8211; one one delimiter can be specified. Download the source from <a href="http://weseetips.com/wp-content/uploads/2010/02/StringStream.zip">here</a>.</p>
<pre>#include "iostream"
#include "sstream"
#include "string"
...

string String = "Long.Live.Visual.C++";
char Seperator = '.';

// Create input string stream.
istringstream StrStream(String);
string Token;

while(getline(StrStream, Token, Seperator))
{
 cout &lt;&lt; Token &lt;&lt; endl;
}
</pre>
<p><span style="text-decoration: underline;"><strong>3) CString::Tokenize()</strong></span></p>
<p>Wanna MFC way? Then CString::Tokenize() is for you. Download the source from <a href="http://weseetips.com/wp-content/uploads/2010/02/CString.zip">here</a>.</p>
<pre>// String.
CString String = _T("long.live_Visual.C++");

// Token seperators.
CString Seperator = _T("._");
int Position = 0;
CString Token;

// Get first token.s
Token = String.Tokenize(Seperator, Position);

while(!Token.IsEmpty())
{
 wcout &lt;&lt; Token.GetBuffer() &lt;&lt; endl;
 Token.ReleaseBuffer();

 // Get next token.
 Token = String.Tokenize(Seperator, Position);
}
</pre>
<p><img class="alignnone size-full wp-image-18" title="Icon Note" src="http://weseetips.com/wp-content/uploads/2008/03/icon_note.jpg" alt="" width="94" height="32" /><br />
Do you know any other mind blowing tokenizing tricks? Then, share with us.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2010/02/11/how-to-tokenize-string-different-tricks-in-trade/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>C++ Union &#8211; Is it still Relevant?</title>
		<link>http://weseetips.com/2010/02/10/c-union-is-it-still-relevant/</link>
		<comments>http://weseetips.com/2010/02/10/c-union-is-it-still-relevant/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 18:37:03 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ union]]></category>
		<category><![CDATA[union]]></category>

		<guid isPermaLink="false">http://weseetips.com/?p=1332</guid>
		<description><![CDATA[Do you remember the old days, where memory was a premium? At that time, unions were used to save memory by merging multiple variables. Gone are the days where we had memory constraints. Now we have GB&#8217;s of RAM itself. So are unions just overhead to language now? Or is it still useful? The merging [...]]]></description>
			<content:encoded><![CDATA[<p>Do you remember the old days, where memory was a premium? At that time, unions were used to save memory by merging multiple variables. Gone are the days where we had memory constraints. Now we have GB&#8217;s of RAM itself. So are unions just overhead to language now? Or is it still useful?</p>
<p><img class="alignnone size-full wp-image-1338" title="CppUnion1" src="http://weseetips.com/wp-content/uploads/2010/02/CppUnion1.png" alt="" width="500" height="261" /></p>
<p><a href="http://weseetips.com/wp-content/uploads/2008/03/icon_howcanidoit.jpg"><img class="alignnone size-full wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.com/wp-content/uploads/2008/03/icon_howcanidoit.jpg" alt="" width="220" height="32" /></a><br />
The merging property of unions can be used for parsing values. For instance have a look at the MessageParser below. If you want to deal with byte streams with specific message format, then unions will be really helpful.</p>
<pre>#pragma pack(1)

// Message layout.
struct MessageLayout
{
 char Signature[3];
 WORD HeaderLen;
 WORD Param1;
 BYTE Param2;
};

// Union to parse header from byte streams.
union MessageHeaderParser
{
 // Byte stream.
 BYTE Bytes[8];
 MessageLayout Layout;
};

int _tmain(int argc, _TCHAR* argv[])
{
 // DWORD Parser.
 BYTE Bytes[] = { 'M','Z', 0, // Header Signature.
                  10,0,       // Header Length
                  20,0,       // WORD param
                  30 };       // BYTE Param.

 MessageHeaderParser Parser;
 memcpy(&amp;Parser.Bytes, Bytes, 8);

 // Get the HIWORD by using standard windows macro.
 cout &lt;&lt; "Signature  :" &lt;&lt; Parser.Layout.Signature &lt;&lt; endl;
 cout &lt;&lt; "Msg Length :" &lt;&lt; Parser.Layout.HeaderLen &lt;&lt; endl;
 cout &lt;&lt; "WORD param :" &lt;&lt; Parser.Layout.Param1 &lt;&lt; endl;
 cout &lt;&lt; "BYTE param :" &lt;&lt; (int)Parser.Layout.Param2 &lt;&lt; endl;

 _getch();
 return 0;
}</pre>
<p><a href="http://weseetips.com/wp-content/uploads/2008/03/icon_note.jpg"><img class="alignnone size-full wp-image-18" title="Icon Note" src="http://weseetips.com/wp-content/uploads/2008/03/icon_note.jpg" alt="" width="94" height="32" /></a></p>
<p>Download the code from <a href="http://weseetips.com/wp-content/uploads/2010/02/Union.zip">here</a>.</p>
<p>BTW, Do you know that the famous Audi brand is a <strong>union </strong>of 4 old legendary car companies? The four rings represents each of the four companies. Interesting, the history is. isn&#8217;t it?</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2010/02/10/c-union-is-it-still-relevant/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++ Function Pointers Simplified!</title>
		<link>http://weseetips.com/2009/10/18/c-function-pointers-simplified/</link>
		<comments>http://weseetips.com/2009/10/18/c-function-pointers-simplified/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 17:53:35 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ function pointers]]></category>
		<category><![CDATA[easy function pointers]]></category>
		<category><![CDATA[function pointer syntax]]></category>
		<category><![CDATA[Function Pointers]]></category>
		<category><![CDATA[how to create function pointer]]></category>
		<category><![CDATA[simple function pointers]]></category>
		<category><![CDATA[typedef function pointer]]></category>
		<category><![CDATA[VC++]]></category>
		<category><![CDATA[Visual C++]]></category>

		<guid isPermaLink="false">http://weseetips.com/?p=1209</guid>
		<description><![CDATA[Background information Pointer is a variable which holds the address of another variable. Where, function pointer is again a variable which holds the address of a function. If you think pointers are evil, then function pointers must be Satan for you. Well, is there any easy way to create function pointers from function prototype? Indeed, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" title="Icon Description" src="http://weseetips.files.wordpress.com/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /></p>
<p><span style="color:#808080;"><em><span style="text-decoration:underline;"><strong>Background information</strong></span><br />
Pointer is a variable which holds the address of another variable. Where, function pointer is again a variable which holds the address of a function. </em></span></p>
<p>If you think pointers are evil, then function pointers must be Satan for you. <img src='http://weseetips.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Well, is there any easy way to create function pointers from function prototype? Indeed, there is. Its the &#8220;BAT&#8221; technique. Never heard about it before? No problem. Its invented by me just now<strong>.</strong><strong> </strong>After watching <span style="text-decoration:underline;"><strong>BatMan</strong></span> series from cartoon network.</p>
<p><img class="alignnone size-full wp-image-1210" title="FunctionPointers" src="http://siteground205.com/~weseetip/wp-content/uploads/2009/10/functionpointers.jpg" alt="FunctionPointers" width="492" height="330" /></p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.files.wordpress.com/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
The &#8220;BAT&#8221; technique is this -</p>
<ol>
<li>Put <strong><span style="color:#0000ff;"><span style="text-decoration:underline;">B</span></span>racket</strong><strong> </strong>or parenthesis around the function name.</li>
<li>Add <strong><span style="color:#0000ff;"><span style="text-decoration:underline;">A</span></span>sterisk </strong>or star in-front of function name.</li>
<li>Now <strong><span style="text-decoration:underline;"><span style="color:#0000ff;">T</span></span>ypedef </strong>it to create a new datatype. Means change the function name to new datatype name and add typedef infront of it.</li>
<li>Now you can use the new function pointer datatype like ordinary variables.</li>
</ol>
<p>For instance, Assume we want to make a function pointer for function &#8211; <strong>DWORD MyFunction( int a, int b)</strong>.</p>
<p><span style="text-decoration:underline;"><strong>1) Bracket</strong></span><br />
<strong> </strong>DWORD <span style="color:#ff0000;"><strong>(</strong></span>MyFunction<span style="color:#ff0000;"><strong>)</strong></span>( int a, int b);</p>
<p><span style="text-decoration:underline;"><strong>2) Asterisk</strong></span><br />
DWORD (<span style="color:#ff0000;"><strong>*</strong></span>MyFunction)( int a, int b);</p>
<p><span style="text-decoration:underline;"><strong>3) Typedef </strong></span><br />
<span style="color:#ff0000;"><strong>typedef</strong></span> DWORD (*MyFunction<span style="color:#ff0000;"><strong>Ptr</strong></span>)( int a, int b);</p>
<p>Ah! you have created a function pointer &#8211; MyFunctionPtr for function type &#8211; &#8216;DWORD MyFunction( int a, int b)&#8217;<br />
Now you can use it like any other variable in your code. For instance, just see the following code snippet with real world usage of function pointers.</p>
<pre>// Callback function for progress notification.
bool NotifyProgress( int Percentage )
{
 // Display progress and return true to continue.
 return true;
}

// typedef function pointer.
typedef bool (*NotifyProgressPtr)( int Percentage );

// DVD Burning function with pointer to NotifyProgress
// to update progress.
void BurnDVD( NotifyProgressPtr FnPtr )
{
 for( int Progress = 0; Progress &lt;= 100; ++Progress )
 {
 // Call the function.
 (*FnPtr)(Progress);
 }
}

// Main function.
int _tmain(int argc, _TCHAR* argv[])
{
 // Ummm... Burn one DVD.
 BurnDVD( NotifyProgress );
 return 0;
}</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.files.wordpress.com/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
Function pointers are not that much evil. Isn&#8217;t it? <img src='http://weseetips.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img class="alignnone size-medium wp-image-51" title="beginnerseries" src="http://weseetips.files.wordpress.com/2008/03/beginnerseries.jpg?w=215" alt="" width="215" height="32" /><br />
Targeted Audience &#8211; Beginners.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2009/10/18/c-function-pointers-simplified/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Delete Duplicate entries from STL containers?</title>
		<link>http://weseetips.com/2009/04/16/how-to-delete-duplicate-entries-from-stl-containers/</link>
		<comments>http://weseetips.com/2009/04/16/how-to-delete-duplicate-entries-from-stl-containers/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 17:43:36 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[C++ unique]]></category>
		<category><![CDATA[delete duplicate entries]]></category>
		<category><![CDATA[remove duplicate entries]]></category>
		<category><![CDATA[set]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[std::set]]></category>
		<category><![CDATA[std::sort]]></category>
		<category><![CDATA[std::unique]]></category>
		<category><![CDATA[unique]]></category>
		<category><![CDATA[Visual C++]]></category>

		<guid isPermaLink="false">http://weseetips.com/?p=996</guid>
		<description><![CDATA[If you want to remove duplicate items, you can go for stl::set. But what to do if you want to delete duplicate data from other containers? Picture Courtesy &#8211; Squidoo You can use std::unique() algorithm to remove adjacent duplicate items. So at first, sort your data, then call std::unique(). Now all the duplicate data will [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" title="Icon Description" src="http://weseetips.wordpress.com/files/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
If you want to remove duplicate items, you can go for <a title="stl::set" href="http://weseetips.com/2008/05/08/seek-uniqueness-use-stl-set/">stl::set</a>. But <span style="color:#0000ff;">what to do if you want to delete duplicate data from other containers?</span></p>
<p><span style="color:#0000ff;"><img class="alignnone size-full wp-image-997" title="removeduplicate" src="http://weseetips.wordpress.com/files/2009/04/removeduplicate.jpg" alt="removeduplicate" width="510" height="309" /><br />
<span style="color:#000000;">Picture Courtesy &#8211; <a href="http://www.squidoo.com/demotivator">Squidoo</a></span><br />
</span></p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.wordpress.com/files/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
You can use <span style="color:#0000ff;">std::unique() </span>algorithm <span style="color:#0000ff;">to remove <span style="text-decoration:underline;"><strong>adjacent duplicate items</strong></span></span>. So at first, <span style="color:#0000ff;">sort your data, then call std::unique()</span>. Now <span style="color:#0000ff;">all the duplicate data will be rearranged to end of container</span>. Now <span style="color:#0000ff;">delete the unwanted range of duplicate data.</span> Have a look at code snippet below.</p>
<pre>#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;

using namespace std;

int main(int argc, char* argv[])
{
    // Election list.
    vector&lt;string&gt; ElectionList;
    ElectionList.push_back( "Sam" );
    ElectionList.push_back( "John" );
    ElectionList.push_back( "Ron" );
    ElectionList.push_back( "Sam" );
    ElectionList.push_back( "John" );

    // Sort the list to make same items be together.
    sort( ElectionList.begin(), ElectionList.end());

    // Rearrange unique items to front.
    vector&lt;string&gt;::iterator Itr = unique(
        ElectionList.begin(),
        ElectionList.end());

    // Delete the duplicate range.
    ElectionList.erase( Itr, ElectionList.end());
}</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.wordpress.com/files/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
Take care that <span style="color:#0000ff;">std::unique() just removes the adjacent duplicate entries</span>. It wont remove the entire duplicate entries present in the container.<span style="color:#0000ff;"> That&#8217;s why we need to sort the container at first</span>, which will arrange all duplicate entries to adjacent  locations. <img src='http://weseetips.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img class="alignnone size-medium wp-image-51" title="beginnerseries" src="http://weseetips.wordpress.com/files/2008/03/beginnerseries.jpg?w=215" alt="" width="215" height="32" /><br />
Targeted Audience &#8211; Beginners.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2009/04/16/how-to-delete-duplicate-entries-from-stl-containers/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Pass Array by Reference?</title>
		<link>http://weseetips.com/2009/03/15/how-to-pass-array-by-reference/</link>
		<comments>http://weseetips.com/2009/03/15/how-to-pass-array-by-reference/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 10:44:55 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[array by ref]]></category>
		<category><![CDATA[array by reference]]></category>
		<category><![CDATA[array reference]]></category>
		<category><![CDATA[error C2234]]></category>
		<category><![CDATA[error C2234: '' : arrays of references are illegal]]></category>
		<category><![CDATA[pass array by ref]]></category>
		<category><![CDATA[pass array by reference]]></category>

		<guid isPermaLink="false">http://weseetips.com/?p=940</guid>
		<description><![CDATA[We used to use arrays, a lot. But did you ever tried how to pass array by reference to another function? Yes. Its a bit tricky. Receiving arrays by reference have special syntax. The arrayname and &#38; symbol should be enclosed in parenthesis. And you should specify the size of array. Have a look at [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" title="Icon Description" src="http://weseetips.files.wordpress.com/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
We used to use arrays, a lot. But did you ever tried <span style="color:#0000ff;">how to pass array by reference </span>to another function? Yes. Its a bit tricky.</p>
<p><img class="alignnone size-full wp-image-941" title="arraybyreference" src="http://siteground205.com/~weseetip/wp-content/uploads/2009/03/arraybyreference.jpg" alt="arraybyreference" width="500" height="471" /></p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.files.wordpress.com/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
Receiving arrays by reference have special syntax. The <span style="color:#0000ff;"><strong>arrayname</strong> </span>and<span style="color:#0000ff;"> <strong>&amp;</strong></span> symbol <span style="color:#0000ff;">should be enclosed in parenthesis.</span> And <span style="color:#0000ff;">you should specify the size of array</span>. Have a look at the following code snippet.</p>
<pre>// Receive Array by reference.
void GetArray( int (&amp;Array) [10] )
{
}

// Test array by reference.
void CRabbitDlgDlg::TestArray()
{
    // Pass array by reference.
    int Array[10] = { 0 };
    GetArray( Array );
}</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.files.wordpress.com/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
Indeed, you can pass the array as pointer and then use it. But if you ever need to pass an array by reference, then remember this tip.</p>
<p><img class="alignnone size-medium wp-image-51" title="beginnerseries" src="http://weseetips.files.wordpress.com/2008/03/beginnerseries.jpg?w=215" alt="" width="215" height="32" /><br />
Targeted Audience &#8211; Beginners.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2009/03/15/how-to-pass-array-by-reference/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to Assert on Object Slicing?</title>
		<link>http://weseetips.com/2009/03/11/how-to-assert-on-object-slicing/</link>
		<comments>http://weseetips.com/2009/03/11/how-to-assert-on-object-slicing/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 18:21:30 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[assert on object slicing]]></category>
		<category><![CDATA[c++ object slicing]]></category>
		<category><![CDATA[detect object slicing]]></category>
		<category><![CDATA[error on object slicing]]></category>
		<category><![CDATA[object slicing]]></category>

		<guid isPermaLink="false">http://weseetips.com/?p=918</guid>
		<description><![CDATA[What is Object Slicing? If derived object is assigned to Base object, then the derived object will be sliced off and only the base part will be copied. Indeed it will cause abnormalities. But is there any mechanism, atleast to assert while object slicing? You can do it by adding an overloaded constructor for derived [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" title="Icon Description" src="http://weseetips.files.wordpress.com/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /></p>
<p><span style="text-decoration:underline;"><strong>What is Object Slicing?<br />
</strong></span><span style="color:#0000ff;">If derived object is assigned to Base object,</span> then the <span style="color:#0000ff;">derived object will be sliced off</span> and only the base part will be copied. Indeed it will cause abnormalities. But is there any mechanism, atleast to assert while object slicing?<br />
<img class="alignnone size-full wp-image-920" title="objectslicing" src="http://siteground205.com/~weseetip/wp-content/uploads/2009/03/objectslicing.jpg" alt="objectslicing" width="510" height="463" /></p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.files.wordpress.com/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
You can do it by <span style="color:#0000ff;">adding an overloaded constructor</span> for derived in Base class and then assert in it. For instance,</p>
<pre>// Forward Declaration.
class Derived;

// Base class.
class Base
{
public:
    // Default Constructor.
    Base() {}
    Base( Derived&amp; derived ) { ASSERT( FALSE ); }
};

// Derived class.
class Derived
{
};
...

// Test code.
Base ObjBase;
Derived ObjDerived;

ObjBase = ObjDerived;</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.files.wordpress.com/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
Take care that it won&#8217;t work for passing pointer and reference. But still good enough. nah?</p>
<p><img class="alignnone size-medium wp-image-53" title="intermediateseries" src="http://weseetips.files.wordpress.com/2008/03/intermediateseries.jpg?w=248" alt="" width="248" height="32" /><br />
Targeted Audiance &#8211; Intermediate.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2009/03/11/how-to-assert-on-object-slicing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Delete Pointers in Vector or Map in Single Line?</title>
		<link>http://weseetips.com/2009/03/02/how-to-delete-pointers-in-vector-or-map-in-single-line/</link>
		<comments>http://weseetips.com/2009/03/02/how-to-delete-pointers-in-vector-or-map-in-single-line/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 19:11:12 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[deallocate map]]></category>
		<category><![CDATA[deallocate vector]]></category>
		<category><![CDATA[delete map]]></category>
		<category><![CDATA[delete vector]]></category>
		<category><![CDATA[for_each]]></category>
		<category><![CDATA[functional header file]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[memory leak map]]></category>
		<category><![CDATA[memory leak vector]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[stl::map]]></category>
		<category><![CDATA[stl::vector]]></category>
		<category><![CDATA[template functor]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://weseetips.com/?p=881</guid>
		<description><![CDATA[If you have two or three STL containers which holds pointers in your class as members, then I&#8217;m sure that its destructor will be the worst readable one. For deallocating STL containers, we have to iterate through each container by for loop, then delete it. But is there any single line function call to delete [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" title="Icon Description" src="http://weseetips.files.wordpress.com/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
If you have two or three STL containers which holds pointers in your class as members, then I&#8217;m sure that its destructor will be the worst readable one. <span style="color:#0000ff;">For deallocating STL containers,</span> we have to <span style="color:#0000ff;">iterate through each container</span> by <span style="color:#0000ff;">for loop,</span> then <span style="color:#0000ff;">delete it.</span> But <span style="color:#0000ff;">is there any single line function call</span> to delete all pointers in vector or map,<strong> <span style="color:#0000ff;">just like chopping the top of a tray of eggs at once?</span></strong> <img src='http://weseetips.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img class="size-full wp-image-882 alignnone" title="deletestlcontainers" src="http://siteground205.com/~weseetip/wp-content/uploads/2009/03/deletestlcontainers.jpg" alt="deletestlcontainers" width="430" height="310" /><br />
Picture Courtesy &#8211; <a title="elitalice.com" href="http://www.elitalice.com/wp-content/uploads/2007/04/eggs.jpg">elitalice.com</a></p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.files.wordpress.com/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
You can use <span style="color:#0000ff;">for_each()</span> and <span style="color:#0000ff;">functors</span> to achieve this. Check out the code snippet.</p>
<p><span style="text-decoration:underline;"><strong>1) How to delete Vector of pointers in single line</strong></span></p>
<pre>// Necessary headers.
#include "functional"
#include "vector"
#include "algorithm"

using namespace std;

// Functor for deleting pointers in vector.
template&lt;class T&gt;
struct DeleteVectorFntor
{
    // Overloaded () operator.
    // This will be called by for_each() function.
    bool operator()(T x) const
    {
        // Delete pointer.
        delete x;
        return true;
    }
};

// Test Function.
void TestVectorDeletion()
{
    // Add 10 string to vector.
    vector&lt;CString*&gt; StringVector;
    for( int Index = 0; Index &lt; 10; ++Index )
    {
        StringVector.push_back( new CString("Hello"));
    }

    // Now delete the vector in a single  line.
    for_each( StringVector.begin(),
              StringVector.end(),
              DeleteVectorFntor&lt;CString*&gt;());
}</pre>
<p><span style="text-decoration:underline;"><strong>1) How to delete Map of pointers in single line</strong></span></p>
<pre>// Necessary headers.
#include "functional"
#include "map"
#include "algorithm"

using namespace std;

// Functor for deleting pointers in map.
template&lt;class A, class B&gt;
struct DeleteMapFntor
{
    // Overloaded () operator.
    // This will be called by for_each() function.
    bool operator()(pair&lt;A,B&gt; x) const
    {
        // Assuming the second item of map is to be
        // deleted. Change as you wish.
        delete x.second;
        return true;
    }
};

// Test function.
void TestMapDeletion()
{
    // Add 10 string to map.
    map&lt;int,CString*&gt; StringMap;
    for( int Idx = 0; Idx &lt; 10; ++Idx )
    {
        StringMap[Idx] = new CString("Hello");
    }

    // Now delete the map in a single  line.
    for_each( StringMap.begin(),
              StringMap.end(),
              DeleteMapFntor&lt;int,CString*&gt;());
}</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.files.wordpress.com/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
STL is really a powerful toolkit. Isn&#8217;t it?</p>
<p><img class="alignnone size-medium wp-image-53" title="intermediateseries" src="http://weseetips.files.wordpress.com/2008/03/intermediateseries.jpg?w=248" alt="" width="248" height="32" /><br />
Targeted Audience &#8211; Intermediate.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2009/03/02/how-to-delete-pointers-in-vector-or-map-in-single-line/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Remove Unreferenced Formal Parameter warning?</title>
		<link>http://weseetips.com/2008/11/26/how-to-remove-unreferenced-formal-parameter-warning/</link>
		<comments>http://weseetips.com/2008/11/26/how-to-remove-unreferenced-formal-parameter-warning/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 19:46:57 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[Project settings]]></category>
		<category><![CDATA[unreferenced formal parameter]]></category>
		<category><![CDATA[UNREFERENCED_PARAMETER]]></category>
		<category><![CDATA[Warning C4100]]></category>
		<category><![CDATA[Warning C4100 unreferenced formal parameter]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=652</guid>
		<description><![CDATA[Hands on legacy code base is a different kind of game! You have to learn a lot of tricks. During old days, legacy codebases have the compiler default settings. But now, for better error catching, most will switch the warning level to high. Well, did you ever ported your legacy codebase by updating its project [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" title="Icon Description" src="http://weseetips.wordpress.com/files/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /></p>
<p><span style="color:#0000ff;">Hands on legacy code base is a different kind of game!</span> You have to learn a lot of tricks. During old days, legacy codebases have the compiler default settings. But now, for better error catching, <span style="color:#0000ff;">most will switch  the warning level to high.</span> Well, did you ever ported your legacy codebase by updating its project settings such as by switching the warning level from Level 3 (default) to Level 4, <span style="color:#0000ff;">most probably you&#8217;ll get this &#8211; <strong>&#8220;Warning C4100: &#8216;Param2&#8242; : unreferenced formal parameter&#8221;</strong>.</span></p>
<p>In most of the case, <span style="color:#0000ff;">it might be some unused stack variable</span>. But in some other cases, <span style="color:#0000ff;">it might be the unused function parameters as well</span>. For instance, if base class have some pure virtual function, we have to implement that, even if we don&#8217;t have any specific implementation for it. In that case, the parameters will not be used and can cause the same warning. Well how to <strong>&#8220;kick out&#8221;</strong> those warnings easily?</p>
<p><img class="alignnone size-full wp-image-660" title="unreferrencedvariablewarning" src="http://weseetips.wordpress.com/files/2008/11/unreferrencedvariablewarning.jpg" alt="unreferrencedvariablewarning" width="510" height="408" /></p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.wordpress.com/files/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
Well, there are two tricks.</p>
<p><span style="text-decoration:underline;"><strong>1) Use the macro &#8211; UNREFERENCED_PARAMETER</strong></span><br />
<span style="color:#0000ff;"> The common trick is to use the macro UNREFERENCED_PARAMETER()</span>. When it expands, <span style="color:#0000ff;">it just assigns the variable to itself. Just like a=a.</span> Which will remove the warning, since the variable is used to assign to itself.</p>
<pre>// The function that derived class was forced to implement,
// since its pure virtual function defined in base.
void CRabbitDialogDlg::Function( bool Param1, bool Param2 )
{
    // Here Param1 and Param2 are not used which will
    // trigger warning. Now they won't.
    UNREFERENCED_PARAMETER( Param1 );
    UNREFERENCED_PARAMETER( Param2 );
}</pre>
<p><span style="text-decoration:underline;"><strong>2) Comment out the variable names.</strong></span><br />
Even though <span style="color:#0000ff;">UNREFERENCED_PARAMETER()</span> serves the need, its <span style="color:#0000ff;">not efficient</span>. isn&#8217;t it? It assigns the variable to itself and thus avoids the warning. Well there is another easy method. <span style="color:#0000ff;">Just comment out the variable name</span>. Check the code snippet below.</p>
<pre>// The function that derived class was forced to implement,
// since its pure virtual function defined in base.
void CRabbitDialogDlg::Function( bool /*Param1*/, bool /*Param2*/ )
{
    // Since the variable name doesn't exist, how compiler can
    // complain that variable is not used. <img src='http://weseetips.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />
}</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.wordpress.com/files/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
In one of my projects, I was forced write an empty implementation for a pure virtual function in derived class. And my client needs zero compilation warning. At that time I really struggled to remove this warning. <img src='http://weseetips.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  <span style="color:#0000ff;">Now when i shared it with you, it feels good!</span></p>
<p><img class="alignnone size-medium wp-image-51" title="beginnerseries" src="http://weseetips.wordpress.com/files/2008/03/beginnerseries.jpg?w=215" alt="" width="215" height="32" /><br />
Targeted Audiance &#8211; Beginners.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/11/26/how-to-remove-unreferenced-formal-parameter-warning/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to Rename Namespace?</title>
		<link>http://weseetips.com/2008/11/23/how-to-rename-namespace/</link>
		<comments>http://weseetips.com/2008/11/23/how-to-rename-namespace/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 22:00:24 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[C++ namespace]]></category>
		<category><![CDATA[C++ rename namespace]]></category>
		<category><![CDATA[namespace]]></category>
		<category><![CDATA[rename namespace]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=646</guid>
		<description><![CDATA[Namspaces are introduced to have logical grouping of classes. But as the framework grows you could often find that the namespace length grows which make the usage more difficult. For instance, one namespace i&#8217;ve encountered is like this - namespace Company { namespace Product { namespace Communication { namespace Event { class CEventEx { }; [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" title="Icon Description" src="http://weseetips.wordpress.com/files/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
<span style="color:#0000ff;">Namspaces are introduced to have logical grouping of classes.</span> But as the framework grows you could often find that the namespace length grows which make the usage more difficult.</p>
<p><img class="alignnone size-full wp-image-649" title="renamenamespaces" src="http://weseetips.wordpress.com/files/2008/11/renamenamespaces.jpg" alt="renamenamespaces" width="403" height="602" /></p>
<p>For instance, one namespace i&#8217;ve encountered is like this -</p>
<pre>namespace Company
{
    namespace Product
    {
        namespace Communication
        {
            namespace Event
            {
                class CEventEx
                {
                };
            };
        }
    };
};</pre>
<p><span style="color:#0000ff;">How much I&#8217;ve to go inside to address a class?</span> Well, in big frameworks this kind of deep hierarchy is unavoidable. But, its a fact that it causes trouble to developer who uses it. Well, is there any idea to avoid that?</p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.wordpress.com/files/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
<span style="color:#0000ff;"> Well, you can rename the namespace to much more smaller one!</span> Have a look at the code snippet below.</p>
<pre>// Rename the namespace
namespace ThirdPartyEvent = Company::Product::Communication::Event;
...
// Create the object by using renamed namespace.
ThirdPartyEvent::CEventEx objEvent;</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.wordpress.com/files/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
There are still a lot of gems inside C++. isn&#8217;t it? <img src='http://weseetips.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img class="alignnone size-medium wp-image-51" title="beginnerseries" src="http://weseetips.wordpress.com/files/2008/03/beginnerseries.jpg?w=215" alt="" width="215" height="32" /><br />
Targeted Audience &#8211; Beginners.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/11/23/how-to-rename-namespace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manage your Constant&#039;s List More Easily By using Enums.</title>
		<link>http://weseetips.com/2008/11/18/manage-your-constants-list-more-easily-by-using-enums/</link>
		<comments>http://weseetips.com/2008/11/18/manage-your-constants-list-more-easily-by-using-enums/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 22:42:05 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Codeproject]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=634</guid>
		<description><![CDATA[List of constants and their count. Every programmer have used it atleast once in his life time. A list of constants can be any for instance, if its an image processing software, it can be a list of image types, the application can manage. And we also used to keep the count of constants present. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" title="Icon Description" src="http://weseetips.files.wordpress.com/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
<span style="color:#0000ff;">List of constants and their count</span>. Every programmer have used it atleast once in his life time. A list of constants can be any for instance, if its an image processing software, it can be a list of image types, the application can manage. And we also used to keep the count of constants present.</p>
<p><img class="alignnone size-full wp-image-639" title="manageconstantlistasenum2" src="http://siteground205.com/~weseetip/wp-content/uploads/2008/11/manageconstantlistasenum2.jpg" alt="manageconstantlistasenum2" width="367" height="470" /></p>
<p>Yes. I know you need an example. Well, lets take the image processing application itself. It might keep a constant list as follows.</p>
<pre>const int IMAGE_TYPE_BMP = 0;
const int IMAGE_TYPE_JPG = 1;
const int IMAGE_TYPE_GIF = 2;
const int IMAGE_TYPE_COUNT = 3;</pre>
<p>Think what all you&#8217;ve to do <span style="color:#0000ff;">if you need to add a new image type?</span> You&#8217;ve to <span style="color:#0000ff;">insert the constant at end, then update the count.</span> Like this,</p>
<pre>const int IMAGE_TYPE_BMP = 0;
const int IMAGE_TYPE_JPG = 1;
const int IMAGE_TYPE_GIF = 2;
const int IMAGE_TYPE_JPG_LOSSY = 3;
const int IMAGE_TYPE_COUNT = 4;</pre>
<p><span style="color:#0000ff;">If you want to append the variable, its okay</span>. You just have to append at end and update the count. <span style="color:#0000ff;">But what if you want to insert the variable at <strong>middle</strong>?</span> In this case JPG_LOSSY seems to be more good just after the JPG. isn&#8217;t it? <span style="color:#0000ff;">In that case you&#8217;ve to update all following constants</span>.Like this,</p>
<pre>const int IMAGE_TYPE_BMP = 0;
const int IMAGE_TYPE_JPG = 1;
const int IMAGE_TYPE_JPG_LOSSY = 2;
const int IMAGE_TYPE_GIF = 3;
const int IMAGE_TYPE_COUNT = 4;</pre>
<p>Assume if your constant list have 50 items! Well, is there any easy trick?</p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.files.wordpress.com/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
Yes! You can <span style="color:#0000ff;">use anonymous enums to avoid the headache.</span> Just declare the constants as follows.</p>
<pre>enum
{
IMAGE_TYPE_BMP = 0,
IMAGE_TYPE_JPG,
IMAGE_TYPE_GIF,
IMAGE_TYPE_COUNT
};</pre>
<p>And if you want to insert another constant at middle, just insert it. You don&#8217;t have to modify anything else. For instance,</p>
<pre>enum
{
IMAGE_TYPE_BMP = 0,
IMAGE_TYPE_JPG,
IMAGE_TYPE_JPG_LOSSY,
IMAGE_TYPE_GIF,
IMAGE_TYPE_COUNT
};</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.files.wordpress.com/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
Since, they are anonymous enums, you can use the constant name directly in code.<br />
Just insert and forget the rest. It will get self adjusted. <img src='http://weseetips.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  Cool! isn&#8217;t it.</p>
<p><img class="alignnone size-medium wp-image-53" title="intermediateseries" src="http://weseetips.files.wordpress.com/2008/03/intermediateseries.jpg?w=248" alt="" width="248" height="32" /><br />
Targeted Audience &#8211; Intermediate.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/11/18/manage-your-constants-list-more-easily-by-using-enums/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

