<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-635788831787428145</id><updated>2011-07-28T09:50:23.113-07:00</updated><category term='scrum'/><category term='good software development'/><category term='w ='/><title type='text'>Notes on Effective C++</title><subtitle type='html'>My notes from my reading of Scott Meyers book on Effective C++ -- -55 ways to improve your programs and design</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>49</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-8611112033364958052</id><published>2007-11-14T12:09:00.000-08:00</published><updated>2007-11-14T12:10:07.479-08:00</updated><title type='text'>test driven development</title><content type='html'>Notes:&lt;br /&gt;&lt;br /&gt;the wikipedia page as usual has good links....here is my notes on them&lt;br /&gt;&lt;br /&gt;http://homepage.mac.com/hey.you/lessons.html : has some good advice on test driven development&lt;br /&gt;http://www.agiledata.org/essays/tdd.html : not bad has some good comparisons and clear up on confusions &lt;br /&gt;&lt;br /&gt;etc.&lt;br /&gt;http://www.agileadvice.com/archives/2005/05/the_qualities_o.html : good one.&lt;br /&gt;http://blog.james-carr.org/?p=44 : types of unit tests that you want to guard against&lt;br /&gt;http://www.agileadvice.com/archives/2005/05/the_qualities_o.html : qualities of a good unit test&lt;br /&gt;http://msdn.microsoft.com/msdnmag/issues/06/01/UnitTesting/default.aspx how to write unit tests &lt;br /&gt;http://weblogs.asp.net/rosherove/archive/2007/10/08/the-various-meanings-of-tdd.aspx interesting to read...kind of philosphical...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-8611112033364958052?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/8611112033364958052/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=8611112033364958052' title='40 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/8611112033364958052'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/8611112033364958052'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/11/test-driven-development.html' title='test driven development'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>40</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-7438474202170225845</id><published>2007-11-12T10:03:00.000-08:00</published><updated>2007-11-14T09:39:45.967-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='good software development'/><category scheme='http://www.blogger.com/atom/ns#' term='scrum'/><title type='text'>Scrum Notes</title><content type='html'>Wikipedia has a good article.&lt;br /&gt;&lt;br /&gt;http://www.softdevarticles.com/modules/weblinks/viewcat.php?cid=46 this page a no of articles the good ones are :&lt;br /&gt;&lt;br /&gt;Adaptive project management using scrum -- I think this is a general primer on scrum. interesting to go thru the slide deck link in the end of the article.&lt;br /&gt;&lt;br /&gt;Agile development lessons learned from the first scrum --feels like a standard primer only....dont think i read it &lt;br /&gt;&lt;br /&gt;its not just stand up...patterns for the daily scrum meeting ---good one...worth reading&lt;br /&gt;&lt;br /&gt;inventing and reinventing scrum in five companies ---feels more like jeff's blah....dont think it is worth it&lt;br /&gt;&lt;br /&gt;when to think about shortening your sprint ..a toolset ---good list of things for which you might want to cut down your sprint length&lt;br /&gt;&lt;br /&gt;Distributed Scrum: agile project management with outsourced development teams --- good to read...since the title is so relevant to us and indicates how they managed their inter continental development teams with language and time constraints.&lt;br /&gt;&lt;br /&gt;looking at other links from the wikipedia page:&lt;br /&gt;the two google video presentations from ken schwaber and jeff sutherland are good to go through....jeff hsa numbers and ken has good stories and nice talk.&lt;br /&gt;&lt;br /&gt;the original paper of the japanese ( the new newproduct development way) is also a good read.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-7438474202170225845?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/7438474202170225845/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=7438474202170225845' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7438474202170225845'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7438474202170225845'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/11/scrum-notes.html' title='Scrum Notes'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-5667013823355829455</id><published>2007-07-16T19:06:00.000-07:00</published><updated>2007-07-16T19:07:11.565-07:00</updated><title type='text'>item 46 and item 47 ...dont really understand them</title><content type='html'>i need to work on them be clear and then sort them out&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-5667013823355829455?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/5667013823355829455/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=5667013823355829455' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5667013823355829455'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5667013823355829455'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/07/item-46-and-item-47-dont-really.html' title='item 46 and item 47 ...dont really understand them'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-4336698069392142400</id><published>2007-07-03T18:18:00.001-07:00</published><updated>2007-07-03T18:18:30.109-07:00</updated><title type='text'>Factor parameter independent code out of templates</title><content type='html'>&lt;span class="gmail_quote"&gt;&lt;/span&gt;   &lt;font face="Arial" size="2"&gt;Commonality and variablity analysis….interesting word for a simple thing that we do&lt;/font&gt;&lt;div&gt;&lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;In non template code repliation is explicit, you can see that there&amp;#39;s duplication between two functions or two classes. In template code replication is imlicit there;s only one copy of the template source code ..but replication happens when the template is instantiated multiple times. &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Non type parameters and type parameters both can cause bloat.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;There are a lot of factors involved in determining performance improvement like size of binary, locaity improvement, cimpiler optimizations, program&amp;#39;s working set size.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;The best way to find the effect is to just try it out on representative data sets and the platforms concerned.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Things to remember:&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Templates generate multiple classes and multiple functions, so any template code not dependent on a template parameter causes bloat.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Bloat due to non type template parameters can often be eliminated by replacing tempalte parameters with function parameters ro class data members.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Bloat due to type aparameters can be reduced by sharing implementations for instantiation types with identical binary representations.&lt;/font&gt;&lt;/p&gt;  &lt;/div&gt; &lt;br&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-4336698069392142400?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/4336698069392142400/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=4336698069392142400' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/4336698069392142400'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/4336698069392142400'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/07/factor-parameter-independent-code-out.html' title='Factor parameter independent code out of templates'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-9148501157543868885</id><published>2007-07-02T17:25:00.001-07:00</published><updated>2007-07-02T17:25:06.772-07:00</updated><title type='text'>use member function templates t o accept all compatible types</title><content type='html'>&lt;p&gt;real pointers support implicit conversions very well.&amp;nbsp; (eg. derived-&amp;gt;base, non const -&amp;gt; const)&lt;/p&gt; &lt;p&gt;iterators into stl containers are almost always smart pointers&lt;/p&gt; &lt;p&gt;class top&lt;/p&gt; &lt;p&gt;class middle : public top&lt;/p&gt; &lt;p&gt;class bottom : public middle&lt;/p&gt; &lt;p&gt;now in templates to ge tmiddle to convert to top we need&lt;/p&gt; &lt;p&gt;template &amp;lt;typename t?&lt;/p&gt; &lt;p&gt;class smartptr {&lt;/p&gt; &lt;p&gt;public:&lt;/p&gt; &lt;p&gt;explicit smartptr(t* realptr);&lt;/p&gt; &lt;p&gt;};&lt;/p&gt; &lt;p&gt;smartptr&amp;lt;top&amp;gt; pt1 = smarptr&amp;lt;middle&amp;gt;(new middle); to get this to compile we will need the corresponding constructor in the smartptr definition, since compilers dont assume any relation between smartpttr&amp;lt;top&amp;gt; and smartptr&amp;lt;middle&amp;gt; classes.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;easiest way to get around is ( but bad)&lt;/p&gt; &lt;p&gt;template typename&amp;lt;t&amp;gt;&lt;/p&gt; &lt;p&gt;class smartptr{&lt;/p&gt; &lt;p&gt;public:&lt;/p&gt; &lt;p&gt;template &amp;lt;typename u&amp;gt;&lt;/p&gt; &lt;p&gt;smartptr(const smartptr&amp;lt;U&amp;gt; &amp;amp; other);,....&lt;/p&gt; &lt;p&gt;}&lt;/p&gt; &lt;p&gt;but this allows converting top object to middle object also.&lt;/p&gt; &lt;p&gt;such functions are called generalized copy constructors. its not declared explicit sive type conversions among built in pointer types.&lt;/p&gt; &lt;p&gt;so what we do is simply use member initialization list to initialize parameters with true ptrs of the other object . then it can compile only if each naked ptr can be converted to the type to which we are trying to convert the object.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;there are more things here that i dont really understand ...&lt;/p&gt; &lt;p&gt;c+= states that if a copy constructor is needed and you dont declare one it will be generated for you ( so if you only have a generalized template than it wont be used by the compiler )&lt;/p&gt; &lt;p&gt;things to remember:&lt;/p&gt; &lt;p&gt;use member function templates to generate functions that accept al lcompatible types&lt;/p&gt; &lt;p&gt;if you declare member templates for generalized copy construction or generalized assignemnt you will still need to declare normal copy constructor and copy assignment operator also.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-9148501157543868885?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/9148501157543868885/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=9148501157543868885' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/9148501157543868885'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/9148501157543868885'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/07/use-member-function-templates-t-o.html' title='use member function templates t o accept all compatible types'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-5763364568925402650</id><published>2007-07-01T20:45:00.000-07:00</published><updated>2007-07-01T21:11:01.472-07:00</updated><title type='text'>know how to access names in templatized base classes</title><content type='html'>when compilers encounter the definition of derived class they dont know what class it inherits from, so they wont find functions defined in the base class, since it doesnt know which base template it is inheriting from ( c++ does this  base may actually be translates to a total template specialization  that doesnt define the particular function in question)&lt;br /&gt;&lt;br /&gt;so basically in templates inheritance stops working.&lt;br /&gt;three ways around this :&lt;br /&gt;&lt;br /&gt;prefix calls to base class functions with this-&gt; ( why does it work ?...as in how does it work underneath?)&lt;br /&gt;use a using declaration e,g,&lt;br /&gt;template &lt;typename company&gt;&lt;br /&gt;class logginmsgsender:public msgsender&lt;company&gt; {&lt;br /&gt;public:&lt;br /&gt;using msgsender&lt;company&gt;::sendclear ; &lt;br /&gt;void sendclearmsg(msginfo&amp; info)&lt;br /&gt;{&lt;br /&gt; sendClear(info);&lt;br /&gt;}&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;or you can exactly specify the function being called in the base class as in &lt;br /&gt;msgsendder&lt;company&gt;::sendclear(info)&lt;br /&gt;&lt;br /&gt;its least desirable because if senclear is virtual than the effect of virtual is lost&lt;br /&gt;&lt;br /&gt;note that all of these solutions basically promise the compiler that the definition of the function will be available, and so when it sees a call to senclearmsg and if it found that the expected function is not available, then it will still give you a compiler error.&lt;br /&gt;&lt;br /&gt;so basically the availablity of a function call or something else is checked only when its usage is encountered.....so if i declare a class that doesnt define sendclear function but neither uses sendclearmsg function than it will compile and execute without error ? is that right ?&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;in derived class templates refer to names in base class templates via a this-&gt;prefix via using declarations or via an explicit base class qualification.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-5763364568925402650?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/5763364568925402650/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=5763364568925402650' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5763364568925402650'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5763364568925402650'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/07/know-how-to-access-names-in-templatized.html' title='know how to access names in templatized base classes'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-5597803186353103720</id><published>2007-07-01T17:56:00.000-07:00</published><updated>2007-07-01T20:45:41.437-07:00</updated><title type='text'>understand the two mieanings of typename</title><content type='html'>names in a template that are dependent on template parameters are called dependent names.&lt;br /&gt;&lt;br /&gt;when a dependent name is nested inside a class it is a nested dependent name.&lt;br /&gt;&lt;br /&gt;nested dependent type name i.e. a nested dependent name that refers to a type&lt;br /&gt;&lt;br /&gt;non dependent names --opposite of this&lt;br /&gt;&lt;br /&gt;if the parser encounters a nested dependent name in a template it assumes that the name is not a type unless you tell it otherwise. by default nested dependent typenames are not types.&lt;br /&gt;so u say: typename c::const_iterator iter(container.begin());&lt;br /&gt;the simple general rule is that anytime you refer to a nested dependent typename in a template you must immediately precede it by the word typename&lt;br /&gt;&lt;br /&gt;exception to above rule is that typename must not precede nested dependent type names in a list of base classes or as a base class identifier in am ember initialization list.&lt;br /&gt;&lt;br /&gt;typedef typename std::iterator_traits&lt;iterT&gt;::value_type value_type&lt;br /&gt;value_type temp(*iter)&lt;br /&gt;&lt;br /&gt;enforcement of the typename rule varies from compiler to compiler.&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;when declaring template parameters class and typename are interchangeable.&lt;br /&gt;use typenames to idnetify nested dependent type names except in base class lists or as a base class identifier in a member initialization list&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-5597803186353103720?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/5597803186353103720/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=5597803186353103720' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5597803186353103720'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5597803186353103720'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/07/understand-two-mieanings-of-typename.html' title='understand the two mieanings of typename'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-2688316828181498853</id><published>2007-07-01T17:38:00.000-07:00</published><updated>2007-07-01T17:56:44.066-07:00</updated><title type='text'>understand implicit interfaces and compile time polymorphism</title><content type='html'>oops revolves around explicit interfaces and runtime polymorphism&lt;br /&gt;&lt;br /&gt;in template programming implicit interface and compile time polymorphism move to the fore.&lt;br /&gt;&lt;br /&gt;basically the point is that the interface that a template class must support is defined by the operations and function calls that has been made on the object instantiated from it.&lt;br /&gt;&lt;br /&gt;so if their is a template class and two different fns are involed on two different instantiations then do both types need definition of that kind of object ?a1.f00() , a2.foo1() ---- does this mean a1.foo1() also has to be defined ?? ..not sure if this quesiton makes sense&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;both classes and templates support interfaces and polymorphism&lt;br /&gt;for classes interfaces are explicit and centered on function signatures, polymorphism occurs at runtime through virtual functions&lt;br /&gt;for template parameters interfaces are implicit and based on valid expressions polymorphism occurs during compilation through template instantiation, and function overloading resolution.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-2688316828181498853?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/2688316828181498853/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=2688316828181498853' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/2688316828181498853'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/2688316828181498853'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/07/understand-implicit-interfaces-and.html' title='understand implicit interfaces and compile time polymorphism'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-2457457142031044597</id><published>2007-07-01T17:17:00.000-07:00</published><updated>2007-07-01T17:38:50.644-07:00</updated><title type='text'>use multiple inheritance judiciously</title><content type='html'>in mi it comes becomes possible to inherit the same name ( function,typedef etc) from more than one base class....potential ambiguity&lt;br /&gt;class borrowableitem{&lt;br /&gt;public:&lt;br /&gt;void checkout();&lt;br /&gt;};&lt;br /&gt;class electronicgadget{&lt;br /&gt;private :&lt;br /&gt;bool checkout() const;&lt;br /&gt;}&lt;br /&gt;class mp3player: public borrowable, public electronicgadget { }&lt;br /&gt;mp3player mp3&lt;br /&gt;&lt;br /&gt;mp3.checkout() is ambigous because to find hte appropriate function to call c++ doesnt worry about its access level.&lt;br /&gt;&lt;br /&gt;deadly diamond&lt;br /&gt;whenever you have diamond think whether their should be only one copy of the base class or it should be one per path.&lt;br /&gt;if you only want one copy of the base class then make the derived classes have a virtual inheritance. e.g.&lt;br /&gt;class file {}&lt;br /&gt;class inputfile : virtual public file {...}&lt;br /&gt;class outputfile: virtual public file {...}&lt;br /&gt;class iofile: public inputfile, public outputfile {,,}&lt;br /&gt;what structure of outputfile was just normal inheritance ?&lt;br /&gt;what if outputfile has virtual private inheritance ?&lt;br /&gt;&lt;br /&gt;STL has a similar class hierarchy in basic_ios, basic_istream , basic_ostream and they are all templates&lt;br /&gt;&lt;br /&gt;the reponsiblity for initializing a virtual base class is born my the most derived class....that means a derived class has to be aware of a virtual base, independent of how far down it is &lt;br /&gt;&lt;br /&gt;try not to use virtual inheritance. if you must try not to put data members in there.&lt;br /&gt;&lt;br /&gt;inheritance is necessary if virtual functions are to be redefined&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;multiple inheritance is more complex than single inheritance., it can lead to new  ambiguity issues and to the need for virtual inheritance.&lt;br /&gt;virtual inheritance imposes costs in size speed and complexity of initialization and assignment. its most practical when virtual base classes have no data.&lt;br /&gt;multiple inheritance does have legitimate uses. one scenario involves combining publid inheritance from an interface class with private inheritance from a class that helps the implementation.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-2457457142031044597?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/2457457142031044597/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=2457457142031044597' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/2457457142031044597'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/2457457142031044597'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/07/use-multiple-inheritance-judiciously.html' title='use multiple inheritance judiciously'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-5853225661172808027</id><published>2007-06-26T18:35:00.001-07:00</published><updated>2007-06-26T18:35:11.218-07:00</updated><title type='text'>Use private inheritance judiciously</title><content type='html'>&lt;br clear="all"&gt;compilers dont convert a derived class object into a base class object automatically if you privately inherit&lt;br&gt;&lt;br&gt;private inheritance means you want to exploit some features available in base class, and not that their is any conceptual relationship between them &lt;br&gt;&lt;br&gt;use composition whenever you can, use private inheritance when you must..genrally when protected or virtual functions are in picture and in edge cases space concerns ....I dont understand this point ?&lt;br&gt;&lt;br&gt;having a composition object inside yourself, you can prevent derived classes from redefining your inherited virtual functions. &lt;br&gt;&lt;br&gt;now edge case: think class with no data ( no non static data , no virtual functions, no virtual bases)such an empty class would have size more than 1 due to c++ technical issues...generally compiler inserts a char, depending on alignment etc you might see different size increase. &lt;br&gt;c++ decrees that free standing objects must have non zero size.&lt;br&gt;class holdsint { int x ; empty e } has size more than int&lt;br&gt;&lt;br&gt;Empty base optimization class holdsint:private empty { int x } has size int....its generally viable under single inheritance only. &lt;br&gt;&lt;br&gt;rules governing c++ object layout generally mean that the ebo cant be applied to derived classes that have more than one base&lt;br&gt;&lt;br&gt;generally empty classes have typedefs enums static data non virts etc.....stl has a no of empty classes....e.g. unary_function and binary_function &lt;br&gt;&lt;br&gt;private inheritance is most likely to be a legitimate design strategy when you are dealing with two classes not related by is a where one either needs a ccess to the protexted members of another or need to redefine one or more of its virtual functions. &lt;br&gt;&lt;br&gt;things to remember:&lt;br&gt;private inheritance means is implemented in terms of . its usually inferior to composition but it makes sense when a derived class needs access to protected base class members or needs to redefine inherited virtual functions &lt;br&gt;&lt;br&gt;uinlike composition privaet inheritance can enable the empty base optimization . this can be important for library developers who strive to minimize object sizes.&lt;br&gt;&lt;br&gt;&lt;br&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-5853225661172808027?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/5853225661172808027/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=5853225661172808027' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5853225661172808027'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5853225661172808027'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/use-private-inheritance-judiciously.html' title='Use private inheritance judiciously'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-1301313016822494415</id><published>2007-06-26T18:00:00.001-07:00</published><updated>2007-06-26T18:00:42.213-07:00</updated><title type='text'>model has a or is implemented in terms of through composition</title><content type='html'>composition is also known as layering, containment, aggregation and embedding&lt;br&gt;&lt;br&gt;their are two kinds of objects....objects in the real world and implementation artifacts ( buffers, mutexes, search trees)&lt;br&gt;&lt;br&gt;things to remember: &lt;br&gt;composition has meanings completely different from that of public inheritance&lt;br&gt;in the application domain, composition means has a , in the implementation domain it means is implemented in terms of .&lt;br&gt;&lt;br&gt;&lt;br&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-1301313016822494415?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/1301313016822494415/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=1301313016822494415' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/1301313016822494415'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/1301313016822494415'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/model-has-or-is-implemented-in-terms-of.html' title='model has a or is implemented in terms of through composition'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-6321470305443473937</id><published>2007-06-26T17:48:00.001-07:00</published><updated>2007-06-26T17:48:02.810-07:00</updated><title type='text'>Never redefine a function's inherited default parameter value</title><content type='html'>   &lt;font face="Arial" size="2"&gt;Virtual functions are dynamically bound whereas default parameter values are statically bound&lt;/font&gt;&lt;div&gt;&lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;I wonder why compilers cannot check for this case themselves ?&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;How will this interact with the case where you don't know the number of arguments beforehand ..not sure even If that is an issue ?&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;You can use the nvi idiom, , public non&amp;nbsp; virtual specifies the default argument and then each private virtual can have its argument. Then it is clear and no redefinition happens in derived&amp;nbsp; Not sure what the author means here ? &lt;/font&gt;&lt;/p&gt; &lt;br&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Things to remember:&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Never redefine an inherited parameter value, because default parameter values are statically bound, while virtual functions --the only functions you should be redefining -- are dynamically bound &lt;/font&gt;&lt;/p&gt;  &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-6321470305443473937?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/6321470305443473937/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=6321470305443473937' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/6321470305443473937'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/6321470305443473937'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/never-redefine-functions-inherited.html' title='Never redefine a function&apos;s inherited default parameter value'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-653919246247648480</id><published>2007-06-26T17:47:00.001-07:00</published><updated>2007-06-26T17:47:27.316-07:00</updated><title type='text'>Never define an inherited non virtual function</title><content type='html'>&lt;span class="gmail_quote"&gt;&lt;/span&gt;   &lt;font face="Arial" size="2"&gt;If mf is non virtual and Base and derived have their own version of this function then&lt;/font&gt;  &lt;br&gt;&lt;div&gt;&lt;p&gt;&lt;font face="Arial" size="2"&gt;D x;&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;B* pb = &amp;amp;x&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;D* pd = &amp;amp;x;&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Pb-&amp;gt;mf() &amp;amp; pd-&amp;gt;mf() will call different functions.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;References exhibit the same baffling behavior as pointers above. I don't really understand this ??&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Things to remember:&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Never redefine an inherited non virtual function&lt;/font&gt; &lt;/p&gt;  &lt;/div&gt; &lt;br&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-653919246247648480?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/653919246247648480/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=653919246247648480' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/653919246247648480'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/653919246247648480'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/never-define-inherited-non-virtual.html' title='Never define an inherited non virtual function'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-5190336845358222939</id><published>2007-06-24T15:55:00.001-07:00</published><updated>2007-06-24T15:55:57.354-07:00</updated><title type='text'>consider alternatives to virtual functions</title><content type='html'>&lt;br clear="all"&gt;the template method pattern via the non virtual interface idiom&lt;br&gt;&amp;nbsp;write a public function that does the before work ( taking a lock, checking conditions) , calls a virtual fn to do the work and than does after work ( logging etc). generally that virtual fn is private ? why? &lt;br&gt;general design is clients call private virtual functions through public non virtual member functions- is known as the non virtual interface idiom.&lt;br&gt;&lt;br&gt;non virtual function is like the virtual function wrapper.&lt;br&gt;note that derived classes redefine private virtual functions ? it says that is ok...but i am not convinced? &lt;br&gt;what if some library class has virtual and then we inherit and accidently override that function.&lt;br&gt;what if you just declare a virtual function without virtual in a derived class without he qualifier ??&lt;br&gt;&lt;br&gt;&lt;br&gt;the strategy pattern via function patterns &lt;br&gt;pass a function pointer with the defined type that is stored. and then call that function.&lt;br&gt;&lt;br&gt;note that using the function pointer to calculate the health means that you need to weaken the encapsulation and provide either public accessors or make it a friend function.  &lt;br&gt;&lt;br&gt;strategy pattern via tr1::function pointer&lt;br&gt;why&amp;nbsp; health calculator shud be a function and not something that just acts like a function.&lt;br&gt;&lt;br&gt;you can use the tr1::bind function to bind an argument to a function. &lt;br&gt;&lt;br&gt;the classic strategy pattern&lt;br&gt;&lt;br&gt;standard pattern.&lt;br&gt;to calculate health of a object , have a separate hierarchy of calculation functions, and keep an object of these calculation functions inside the object.&lt;br&gt; &lt;br&gt;summary:&lt;br&gt;use the non virtual interface idiom. a form of the template method design pattern that wraps public non virtual member functions around less accessible virtual functions.&lt;br&gt;&lt;br&gt;replace virtual functions with function pointer data members, a stripped down manifestation of the strategy design pattern &lt;br&gt;&lt;br&gt;replace virtual functions with tr1::function data members thus allowing use of any callable entity with a signature compatible with what you need. this too is a form of the strategy design pattern&lt;br&gt;&lt;br&gt;replace virtual functions in one hierarchy with virtual functions in another hierarchy. this is the conventional implementation of the strategy design pattern &lt;br&gt;&lt;br&gt;things to remember:&lt;br&gt;alternative to virtual functions include the nvi idiom and various forms of the strategy desing pattern the nvi idiom is itself an example of the template design pattern&lt;br&gt;&lt;br&gt;a disadvantage of moving functionality from a member function to a function outside the class is that the non member function lacks access to the class&amp;#39;s non public members &lt;br&gt;&lt;br&gt;tr1::function objects act like generalized function pointers, such object support all callable entities compatible with a given target signature&lt;br&gt;&lt;br&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-5190336845358222939?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/5190336845358222939/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=5190336845358222939' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5190336845358222939'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5190336845358222939'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/consider-alternatives-to-virtual.html' title='consider alternatives to virtual functions'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-5604070828130327209</id><published>2007-06-24T11:37:00.001-07:00</published><updated>2007-06-24T11:37:51.803-07:00</updated><title type='text'>differnentiate between inheritance of interface and inheritance of implementation</title><content type='html'>&lt;br clear="all"&gt;the difference between these two is same as the difference between function declaration and function definition.&lt;br&gt;&lt;br&gt;member function interfaces are always inherited.public inheritance means that trivially &lt;br&gt;the purpose of declaring a pure virtual function is to havea a dervied class inherit a function interface only.&lt;br&gt;&lt;br&gt;you can provide a definition for a pure function also, but not a ver useful thing in general.&lt;br&gt;&lt;br&gt; the purpose of declaring&amp;nbsp; simple virtual functions is to have derived classes inherit a function interface as well as a default implementation.&lt;br&gt;&lt;br&gt;to avoid the chance of forgetting to define a proper version when a new class comes in, then you can define a function in the interface that is pure virtual and have it call a portected virtual function to do the actual thing.  &lt;br&gt;&lt;br&gt;that way every new class has to define the virtual function and it gets you&lt;br&gt;a non virtual member function specifies and invariant over specialization&lt;br&gt;&lt;br&gt;pure virtual - interface only&lt;br&gt;simple virtual --interface and&amp;nbsp; default implementation &lt;br&gt;non virtual -- interface and a mandatory implementation&lt;br&gt;&lt;br&gt;things to remember:&lt;br&gt;inheritance of interface is different from inheritance of implementation. under public inheritance derived calsses always inherit base class interfaces. &lt;br&gt;pure virtual functions specift inheritance of interface only&lt;br&gt;simple virtual functions specify inheritance of interface plus inheritance of a default implementation.&lt;br&gt;non virtual functions specift inheritance of interface plus inheritance of a mandatory implementation &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-5604070828130327209?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/5604070828130327209/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=5604070828130327209' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5604070828130327209'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5604070828130327209'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/differnentiate-between-inheritance-of.html' title='differnentiate between inheritance of interface and inheritance of implementation'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-4250103335308121630</id><published>2007-06-24T09:59:00.001-07:00</published><updated>2007-06-24T09:59:28.500-07:00</updated><title type='text'>avoid hiding inherited names</title><content type='html'>&lt;span class="gmail_quote"&gt;&lt;br&gt;&lt;/span&gt;scope a derived class is nested inside its base class&amp;#39;s scope&lt;br&gt;&lt;br&gt;only names matter for determining what gets hidden &lt;br&gt;so if u define a fn in base and same name is defined in derived than that name will hide all the names in base. &lt;br&gt;&lt;br&gt;if you want to carry over all names from base , use using base::name&lt;br&gt;is this behavior same for virtual functions also ? does their signature have to be exactly same ? check on this ?&lt;br&gt;&lt;br&gt;if u want to enable only a few variants from the overload, than just have a forwarding function. &lt;br&gt;how do you do this if you want to bring in a variable definition in scope ?&lt;br&gt;&lt;br&gt;things to remember:&lt;br&gt;names in derived classes hide names in base classes. under public inheritance this is never desirable.&lt;br&gt;to make hidden names visible again, eploy using declarations or forwarding functions &lt;br&gt;&lt;br&gt; &lt;br clear="all"&gt;&lt;br&gt;-- &lt;br&gt;Regards,&lt;br&gt;Umesh Kumar&lt;br&gt;&lt;br&gt;&lt;a href="http://worthyarticles.blogspot.com/"&gt;http://worthyarticles.blogspot.com/&lt;/a&gt;&lt;br&gt;&lt;a href="http://learnbooks.blogspot.com/"&gt;http://learnbooks.blogspot.com/ &lt;/a&gt;&lt;br&gt;&lt;a href="http://cplusplusnotes.blogspot.com/"&gt;http://cplusplusnotes.blogspot.com/&lt;/a&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-4250103335308121630?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/4250103335308121630/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=4250103335308121630' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/4250103335308121630'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/4250103335308121630'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/avoid-hiding-inherited-names.html' title='avoid hiding inherited names'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-4869221749872891874</id><published>2007-06-24T09:47:00.001-07:00</published><updated>2007-06-24T09:47:51.858-07:00</updated><title type='text'>make sure public inheritance models is a relationship</title><content type='html'>a derived class is a specialized version of base class so it should follow all constraints and provide all functionalities of the base class.&lt;br&gt;&lt;br&gt;so pre conditions of dervied shud be subset of pre conditions of base&lt;br&gt; post conditions shud be superset of post conditions&lt;br&gt;&lt;br&gt;differentiate between penguins cant fly and penguins can fly, but it is actually an error to make them fly.&lt;br&gt;&lt;br&gt;things to remember:&lt;br&gt;public inheritance means is a . everything that applies to base classes must also apply to derived classes, because every derived class object is a base class objects &lt;br clear="all"&gt;&lt;br&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-4869221749872891874?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/4869221749872891874/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=4869221749872891874' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/4869221749872891874'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/4869221749872891874'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/make-sure-public-inheritance-models-is.html' title='make sure public inheritance models is a relationship'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-849283320116556016</id><published>2007-06-21T20:21:00.003-07:00</published><updated>2007-06-21T20:21:55.473-07:00</updated><title type='text'>Minimize compilation dependencies between files</title><content type='html'>&lt;font face="Arial" size="2"&gt;In general a class specifies not just interface but a fair no of implementation details. Like private variables, inline functions etc.&lt;/font&gt;&lt;div&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Including standard headers is unlikely to be the problem as you can take advantage of precompiled headers.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;You can use the pimpl idiom to get around with hiding the impl from the clients&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;I don't understand what is the meaning of replace dependencies on definitions with dependencies on declarations. ????&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;This minimizes compilation dependencies.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Two rules to follow:&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Avoid using objects when object references and pointers will do&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Depend on class declarations instead of class definitions whenever you can. ---I don't understand the note about moving dependencies to client&amp;#39;s files at bottom of page 143 ???&lt;/font&gt;&lt;/p&gt;   &lt;p&gt;&lt;font face="Arial" size="2"&gt;Provide separate header files for declarations and definitions.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Look at iosfwd header file.???&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Handle classes are classes that employ the pimpl idiom&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;They generally forward all their calls to the base class.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Or you can have an interface class exposed to client and actual class that implements the interface. Then you provide a static factory function that gives the actual class ( typically a shared pointer to it). &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Obviously both of these approaches have the cost of most fn calls being virtual function calls.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Things to remember:&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;The general idea behind minimizing compilation dependencies is to depend on declarations instead of definitions. Two approaches based on this idea are handle classes and interface classes. &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Library header files should exist in full and declaration only forms,. This applies regardless of whether templates are involved or not &lt;/font&gt;&lt;/p&gt;  &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-849283320116556016?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/849283320116556016/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=849283320116556016' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/849283320116556016'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/849283320116556016'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/minimize-compilation-dependencies.html' title='Minimize compilation dependencies between files'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-1585080797018327384</id><published>2007-06-21T20:21:00.001-07:00</published><updated>2007-06-21T20:21:22.490-07:00</updated><title type='text'>Understand the ins and outs of inlining</title><content type='html'>&lt;font face="Arial" size="2"&gt;Compilers are designed to optimize nicely over stretches of code without function calls.&lt;/font&gt;&lt;div&gt;&lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Inlining can cause code bloat -&amp;gt; lower hit rates,memory crunch -&amp;gt; bad performance&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Define function inside a class also makes it inline.&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Inline functions should typically be in header files. Since inlining is generally a compile type thing&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Compilers choose whether to inline or not&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;All but the trivial virtual fns are generally not inlined.&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Most compilers have a diagnostic level that will result in a warning if compiler cant mark a function inline.&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;If your program takes the address of a function then the body of the function wud be generated even if compiler can imline it. Even if you don't do it ,sometimes compilers might just do that. &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Compilers put hteir own code in constructors and destructors for handling exception, destruction In case of incomplete construction, calling base class constructor for derived classes etc.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;If u have a inline function then clients are using it&amp;nbsp; means it is in there code, so u change implementation of that fn means clients have to recompile.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;One pain in coding is that debuggers have trouble with inline functions&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Things to remember:&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Limit most inlinking to small frequently called functions. This facilititates debugging and binary upgradablity, minimizes potential code bloat, and mizimizes the changes of greater program speed. &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Don't declare function templates inline just because they appeat in header files&lt;/font&gt; &lt;/p&gt;  &lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-1585080797018327384?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/1585080797018327384/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=1585080797018327384' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/1585080797018327384'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/1585080797018327384'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/understand-ins-and-outs-of-inlining.html' title='Understand the ins and outs of inlining'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-3006170165928756596</id><published>2007-06-21T20:20:00.001-07:00</published><updated>2007-06-21T20:20:41.491-07:00</updated><title type='text'>Strive for exception safe code</title><content type='html'>&lt;font face="Arial" size="2"&gt;When exceptions are thrown, foolowng should happen:&lt;/font&gt;  &lt;br&gt;&lt;div&gt;&lt;p&gt;&lt;font face="Arial" size="2"&gt;&amp;nbsp;Leak no resources ---use resource classes to manage all resources. Such classes also make code shorter.&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Don't allow data structures to become corrupted---don't be left pointing to a deleted object,&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Exception safe functions offer one of the following guarantees:&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Basic guarantee --- exception thrown means program will remain in a valid state ( invariants satisfied etc)though exact state maybe unknown&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Strong guarantee -- exception thrown means program state is unchanged. Calls to such functions are atomic…so boolean success or failure&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;NoThrow guarantee-- never throw exceptions. All operations on built in types are no throw.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Exception safe code always offers one of the three guarantees above. If not its not exception safe.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Int doSomething() throw() ---this doesn't mean no exc thrown, but that if a exception is thrown means serious error and call unexpected fun.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Try strongest guarantee but very hard.Strong guarantee is pretty hard because something could have changed state and you don't know what change it called&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;The general design strategy that you can use to get strong guarantee is copy , change copy&amp;nbsp; and swap in a non throwing oepration….obviously its costly and maynot give strong guarantee.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Normal : real obj = real obj { ptr to real data} --pimpl idiom&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;A functions exception safety guarantee is part of its interface….so give it as much weight as to other parts of its interface.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Things to remember:&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Exception safe functions leak no resources and allow no data structures to become corrupted, even when exceptions are thjrown. Such functions offer the basic, strong on nothrow guarantees. &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;The strong guarantee can often be implemented via copy and swap, but the storng guarantee is not practical for all functions.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;A function can usually offer a guarantee no stronger than the weakest guarantee of the functions it calls.&lt;/font&gt; &lt;/p&gt; &lt;br&gt;&lt;/div&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-3006170165928756596?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/3006170165928756596/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=3006170165928756596' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3006170165928756596'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3006170165928756596'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/strive-for-exception-safe-code.html' title='Strive for exception safe code'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-5645548548995790644</id><published>2007-06-19T19:34:00.000-07:00</published><updated>2007-06-19T19:48:53.209-07:00</updated><title type='text'>avoid returning handles to object internals</title><content type='html'>class rectangle {&lt;br /&gt;public:&lt;br /&gt;Point&amp; upperLeft() const { return pData-&gt;ulhc; }&lt;br /&gt;Point&amp; lowerRight() const { return pData-&gt;lrhc;}&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;will this compile ? isnt it wrong that this compiles ?&lt;br /&gt;&lt;br /&gt;if a const member function returns a reference to data associated with an object that is stored outside the object itself, the caller of the function can modify that data....this a fallout of the limitations of bitwise constness.&lt;br /&gt;&lt;br /&gt;reference pointers iterators are all handles and all have the same issue,&lt;br /&gt;&lt;br /&gt;returning handle to objects internals always runs the risk of compromising encapsulation.&lt;br /&gt;&lt;br /&gt;private fns are also object internals.&lt;br /&gt;&lt;br /&gt;use const point&amp; upperleft() cosnt { return pdata-&gt;ulhc; } solves the problem above.&lt;br /&gt;&lt;br /&gt;returning handles can also lead to dangling handles. handles that refer to parts of objects that dont exist.... e.g. ( very similar to window example in previous item)&lt;br /&gt;&lt;br /&gt;class guiobject{&gt;&gt;&gt;}&lt;br /&gt;const rectangle boundingBox (const guiobject&amp; g);&lt;br /&gt;&lt;br /&gt;guiobject * pgo;&lt;br /&gt;const point* pupperleft  = &amp;( boundingbox(*pgo).upperleft());&lt;br /&gt;a temp obj is former, upper kleft taken and then it gets destroyed.&lt;br /&gt;&lt;br /&gt;so dont return object internal data ( their are notable exceptions like operator[] )&lt;br /&gt;&lt;br /&gt;but it shud be an exception not a rule&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;avoid returning handles ( references , pointers, iterators) to object internals, not returning handles increases encapsulation helps const member functions act const and minimizes the creation of dangling handles.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-5645548548995790644?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/5645548548995790644/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=5645548548995790644' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5645548548995790644'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5645548548995790644'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/avoid-returning-handles-to-object.html' title='avoid returning handles to object internals'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-7155121128472875147</id><published>2007-06-19T18:56:00.000-07:00</published><updated>2007-06-19T19:29:28.908-07:00</updated><title type='text'>Minimize casting</title><content type='html'>rules of c++ are designed to guarantee that type errors are impossible.&lt;br /&gt;&lt;br /&gt;casting is more necessary and less dangerous in c and java than c++. approach it carefully in c++&lt;br /&gt;&lt;br /&gt;old style casts: (T) expression  , T(expression) ---both are exactly same&lt;br /&gt;&lt;br /&gt;are these really same....i think second one invokes the defined conversion operator ...whereas first one just causes reinterpretation????&lt;br /&gt;&lt;br /&gt;newer casts &lt;br /&gt;const_cast&lt;t&gt;( expression)&lt;br /&gt;dynamic_cast&lt;t&gt;(expression)&lt;br /&gt;reinterpret_cast&lt;t&gt;(expression)&lt;br /&gt;static_Cast&lt;t&gt;(expression)&lt;br /&gt;&lt;br /&gt;dynamic cast may have a  significant runtime cost&lt;br /&gt;reinterpret_cast means trouble, low level use only, results in unportable code&lt;br /&gt;&lt;br /&gt;i dont understand why this should work ? ???&lt;br /&gt;class widget {&lt;br /&gt;public:&lt;br /&gt; explicit widget(int size);&lt;br /&gt;};&lt;br /&gt;dosomework( static_cast&lt;widget&gt;(15));&lt;br /&gt;&lt;br /&gt;how does this work with that constructor is explicit ???&lt;br /&gt;&lt;br /&gt;derived d ;&lt;br /&gt;base* bp = &amp;d;&lt;br /&gt;bp and &amp;d maynt be same ...in both single inheritance and virtual inheritance&lt;br /&gt;this cant happen in c and java and c#.&lt;br /&gt;&lt;br /&gt;can operators be virtual functions?&lt;br /&gt;&lt;br /&gt;class window {&lt;br /&gt;public:&lt;br /&gt;virtual void onResize() { ...}&lt;br /&gt;};&lt;br /&gt;class specialwindow:public window {&lt;br /&gt;public: &lt;br /&gt;virtual void onResize(){&lt;br /&gt;  static_cast&lt;window&gt;(*this).onResize();&lt;br /&gt;}&lt;br /&gt;};&lt;br /&gt;onresize doesnt call onresize on the base object. it makes a copy of the base object part and calls it on that object. use window::onResize for the effect.&lt;br /&gt;&lt;br /&gt;cast creates a new temporary copy of the base class part.&lt;br /&gt;&lt;br /&gt;if you find urself wanting to case its a sign that you could be approaching things the wrong way.&lt;br /&gt;&lt;br /&gt;dynamic cast:&lt;br /&gt;it can be quite slow.&lt;br /&gt;one common implementation is based in part on string comparison of class names&lt;br /&gt;s oa single 4 level hierarchy means upto 4 comparisons for each cast.&lt;br /&gt;&lt;br /&gt;deeper or multiple inheritance hierarchies are more costly....its implemented this way to support dynamic linking&lt;br /&gt;be afraid of dynamic cast in performance sensitive code.&lt;br /&gt;&lt;br /&gt;you can maybe define a virtual function to avoid casts or have only single type of objects in a container to avoid it.&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;avoid casrs whenever practical, esp dynamic casts in performance sensitive code. if a design requires casting, try to develop a cast free alternative.&lt;br /&gt;&lt;br /&gt;when casting is necessary try to hide it inside a function. clients can then call the function instead of putting casts in their own code&lt;br /&gt;&lt;br /&gt;prefer c++ style casts to old style casts. they are easier to see and they are more specific about what they do.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-7155121128472875147?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/7155121128472875147/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=7155121128472875147' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7155121128472875147'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7155121128472875147'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/minimize-casting.html' title='Minimize casting'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-8191101671793396819</id><published>2007-06-19T18:48:00.000-07:00</published><updated>2007-06-19T18:56:48.746-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='w ='/><title type='text'>postpone variable definition as long as possible</title><content type='html'>you dont want to incur the cost of variable creation and destruction if you are not going to use it completely.&lt;br /&gt;dont ignore cost of creation if their are code paths where that variable wouldnt be used.&lt;br /&gt;&lt;br /&gt;try to delay declaration till the time value is known.&lt;br /&gt;then initialize and declare in the same statement, so that the cost of default construction of object is saved.&lt;br /&gt;&lt;br /&gt;cost analysis in a loop:&lt;br /&gt;widget w ;&lt;br /&gt;for ( int i=0;i &lt; n ; i++)&lt;br /&gt;    w = &lt;br /&gt;&lt;br /&gt;OR &lt;br /&gt;for ( int i=0;i &lt; n ; i++)&lt;br /&gt;   widget w &lt;br /&gt;&lt;br /&gt;cost  = 1 constructor + 1 destructor + n assignments&lt;br /&gt;cost  = n constructors + n destructors&lt;br /&gt;&lt;br /&gt;pick whichever is better....note that second one is slightly more tight in that w isnt visible outside loop&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;postpone variable definitions as long as possible. It increases program clarity and improves program efficiency&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-8191101671793396819?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/8191101671793396819/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=8191101671793396819' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/8191101671793396819'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/8191101671793396819'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/postpone-variable-definition-as-long-as.html' title='postpone variable definition as long as possible'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-3093745082291490762</id><published>2007-06-16T20:12:00.001-07:00</published><updated>2007-06-16T20:12:35.602-07:00</updated><title type='text'>Consider support for a non throwing swap</title><content type='html'>&lt;p&gt;so u can use the default implementation of swap. or you can provide a total specialization of a template to provide a more efficient version of swap.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;the general idea is that the class provides a public swap function and then we provide a total specialization of the swap template of std namespace.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;this is the trend in stl too, all containers ( vector, list etc) provide a public swap function and a total specialization of the general swap template.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;c++ allows partial specialization of class templates but not partial specialization of function templates&lt;/p&gt; &lt;p&gt;template&amp;lt; class t&amp;gt;&lt;/p&gt; &lt;p&gt;swap &amp;lt;widget&amp;lt;t&amp;gt;&amp;gt; ( widget &amp;lt;t&amp;gt; &amp;amp; a , widget&amp;lt;t&amp;gt;&amp;amp;b) {..call fn defined in class).}&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;this doesnt compile&lt;/p&gt; &lt;p&gt;the way to get around this is you define the swap you want as before but in the namespace of your class ( assuming class is not in std namespace) then after that c++ name lookup rules will take care of picking the right implementation for you ( koenig rule ,argument dependent lookup)&lt;/p&gt; &lt;p&gt;&amp;nbsp;pimpl idiom is that you keep a pointer to implementation inside the object&lt;/p&gt; &lt;p&gt;make sure that your public member swap function shudnt throw an exception.&amp;nbsp;i&amp;nbsp;dont understand why ????&lt;/p&gt; &lt;p&gt;what is strong exception safety guarantee ???&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;from a client perspective if you are calling swap, then use a using declaration for general swap and make the actual call as swap (...) and not std::swap(...)&lt;/p&gt; &lt;p&gt;things to remember:&lt;/p&gt; &lt;p&gt;provide a swap member function, when std::swap would be inefficient for your type make sure your swap doesn't throw exceptions&lt;/p&gt; &lt;p&gt;if you offer a member swap also offer a non member swap that calls the member for classes ( not templates) specialize std::Swap too&lt;/p&gt; &lt;p&gt;when calling swap employ a using declaration for std::Swap then call without namespace qualification&lt;/p&gt; &lt;p&gt;its fine to totally specialize std templates for user defined types , but never try to add something completely new to std&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-3093745082291490762?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/3093745082291490762/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=3093745082291490762' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3093745082291490762'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3093745082291490762'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/consider-support-for-non-throwing-swap.html' title='Consider support for a non throwing swap'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-8807507270401390623</id><published>2007-06-16T15:55:00.001-07:00</published><updated>2007-06-16T15:55:06.613-07:00</updated><title type='text'>Declare non member functions when type conversion should apply to all parameters</title><content type='html'>&lt;p&gt;parameters are eligible for implicit type conversion only if they are listed in the parameter list of the function.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;i am confused on this point, since i read a lot in strousstrup also on this and i don't remember what&amp;nbsp; all i read.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;the opposite of a member function is a non member function not a friend function&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;read this funda there ?&lt;/p&gt; &lt;p&gt;things to remember:&lt;/p&gt; &lt;p&gt;if you need type conversions on all parameters to a function ( including the one that would otherwise&amp;nbsp; be pointed to by the this pointer) the function must be a non member function.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-8807507270401390623?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/8807507270401390623/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=8807507270401390623' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/8807507270401390623'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/8807507270401390623'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/declare-non-member-functions-when-type.html' title='Declare non member functions when type conversion should apply to all parameters'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-7303617713071703203</id><published>2007-06-16T15:47:00.001-07:00</published><updated>2007-06-16T15:47:05.798-07:00</updated><title type='text'>prefer non member non friend functions to member functions</title><content type='html'>&lt;p&gt;if the function is just gathering functionality provided by other member functions which are actually interacting with class members etc... than you can consider making it a function in same namespace.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;the more member functions or friend functions more code is possibly affected if you change the class implementation is changed.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;this approach also reduces compilation issues since clients which use only one convenience function don't have to be recompiled on addition of another convenience function to namespace etc.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;c++standard library is also organized the same way . their isnt a #include&amp;lt;c++standardlibrary&amp;gt; but #include &amp;lt;vector&amp;gt;&amp;lt;lit&amp;gt; .....&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;partitioning functionality in this way is not possible when it comes from a class's member functions because a class must be defined in its entirety it cant be split into pieces&lt;/p&gt; &lt;p&gt;this also allows clients to add convenience functions if they want.&lt;/p&gt; &lt;p&gt;things to remember:&lt;/p&gt; &lt;p&gt;prefer non member non friend functions to member functions. doing so increases encapsulation, packaging flexibility and functional extensibility.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-7303617713071703203?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/7303617713071703203/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=7303617713071703203' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7303617713071703203'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7303617713071703203'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/prefer-non-member-non-friend-functions.html' title='prefer non member non friend functions to member functions'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-6319532388556597864</id><published>2007-06-16T15:31:00.001-07:00</published><updated>2007-06-16T15:31:45.320-07:00</updated><title type='text'>declare data members private</title><content type='html'>&lt;p&gt;if everything in public interface is a function than clients don't have to think whether to put parenthesis or not when referring to api&lt;/p&gt; &lt;p&gt;encapsulation is the big reason ....if data members are private they can be replaced with computations/function calls/ different member&amp;nbsp; (say normal ptr by tr1 ptr) later on&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;this will let you notify other objects when data members are read or written and check port and pre conditions or other things if you want to.&lt;/p&gt; &lt;p&gt;practically encapsulated means unchangeable.....&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;protected data members are bad for similar reasons.&lt;/p&gt; &lt;p&gt;protected is no more encapsulated than public ..i don't understand this???&lt;/p&gt; &lt;p&gt;things to remember:&lt;/p&gt; &lt;p&gt;declare data members private. it gives clients syntactically uniform access to data members affords fine grained access control , allows invariant's to be enforced and offers class authors implementation flexibility.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;protected is no more encapsulated than public &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-6319532388556597864?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/6319532388556597864/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=6319532388556597864' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/6319532388556597864'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/6319532388556597864'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/declare-data-members-private.html' title='declare data members private'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-3554061073035699456</id><published>2007-06-16T15:01:00.001-07:00</published><updated>2007-06-16T15:01:40.176-07:00</updated><title type='text'>Test post from Windows Live Writer</title><content type='html'>&lt;p&gt;To see if&amp;nbsp; things go correctly or not&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-3554061073035699456?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/3554061073035699456/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=3554061073035699456' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3554061073035699456'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3554061073035699456'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/test-post-from-windows-live-writer.html' title='Test post from Windows Live Writer'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-2620278871445495941</id><published>2007-06-16T14:57:00.001-07:00</published><updated>2007-06-16T14:57:16.788-07:00</updated><title type='text'>don't try to return a reference when you must return an object</title><content type='html'>&lt;br clear="all"&gt;basically you might be returning reference to a object created locally in the function and that can cause all sorts of undefined behavior.&lt;br&gt;&lt;br&gt;think if you really get a benefit from returning a reference to an object that you are creating in the function....also you need to worry who will free it. sometimes you mayn&amp;#39;t even be able to get to freeing it.( think&amp;nbsp; w = a*b*c ) and operator&amp;nbsp; * returns a reference to a&amp;nbsp; object created on heap.....how do u get to temp created in a*b and the second temp created in temp*c  &lt;br&gt;&lt;br&gt;returning static objects is also awful....think multi threaded programs, think ( foo(i)*foo(j) == foo(k) * foo(l) )&amp;nbsp; will always return true ( because both side point to same static object)&lt;br&gt;&lt;br&gt;static arrays are also a bad choice. &lt;br&gt;&lt;br&gt;so if your function must return a new object than let that function return a new object, don&amp;#39;t use reference or some other trick to try to get some performance benefit.&lt;br&gt;&lt;br&gt;compilers can do lots of optimization to reduce/eliminate the overhead when it is possible. &lt;br&gt;&lt;br&gt;so be sane.&lt;br&gt;things to remember:&lt;br&gt;never return a pointer or a reference to a local stack object. a reference to a heap allocated object or a pointer or reference to a local static object if there is a change that more than one such subject will be needed. &lt;br&gt;&lt;br&gt;&lt;br&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-2620278871445495941?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/2620278871445495941/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=2620278871445495941' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/2620278871445495941'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/2620278871445495941'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/dont-try-to-return-reference-when-you.html' title='don&apos;t try to return a reference when you must return an object'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-5723132518218372034</id><published>2007-06-16T14:38:00.001-07:00</published><updated>2007-06-16T14:38:22.169-07:00</updated><title type='text'>prefer pass by reference to const to pass by value</title><content type='html'>&lt;span class="gmail_quote"&gt;&lt;/span&gt;&lt;br clear="all"&gt;default is pass by value.fn parameters are initialized with copied of actual parameters.&lt;br&gt;&lt;br&gt;passing params by refernce avoids the slicing problem.&lt;br&gt;when a derived class object is passed as a base class object, then the base class constructor is called,and derived class parts are lost.... that means virtual functions will resolve to base class inside called function ? &lt;br&gt;&lt;br&gt;i want to test it out in code.&lt;br&gt;&lt;br&gt;for built in types, stl iterators and types for which you know that pass by value is inexpensive , function object types you can use pass by value ( though you wanna be sure that the size of these types wont become large in future). &lt;br&gt;&lt;br&gt;what are function object types and why is pass by value appropriate for them ?&lt;br&gt;things to remember:&lt;br&gt;prefer pass by reference to const over pass by value. its typically more efficient and it avoids the slicing problem &lt;br&gt;&lt;br&gt;the rule doesn&amp;#39;t apply to built in types and stl iterators and function object types for them pass by value is usually appropriate&lt;br&gt;&lt;br&gt;&lt;br&gt; &lt;br clear="all"&gt;&lt;br&gt;-- &lt;br&gt;Regards,&lt;br&gt;Umesh Kumar&lt;br&gt;&lt;br&gt;&lt;a href="http://worthyarticles.blogspot.com/"&gt;http://worthyarticles.blogspot.com/&lt;/a&gt;&lt;br&gt;&lt;a href="http://learnbooks.blogspot.com/"&gt;http://learnbooks.blogspot.com/ &lt;/a&gt;&lt;br&gt;&lt;a href="http://cplusplusnotes.blogspot.com/"&gt;http://cplusplusnotes.blogspot.com/&lt;/a&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-5723132518218372034?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/5723132518218372034/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=5723132518218372034' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5723132518218372034'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5723132518218372034'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/prefer-pass-by-reference-to-const-to.html' title='prefer pass by reference to const to pass by value'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-7462648531657768646</id><published>2007-06-16T14:37:00.001-07:00</published><updated>2007-06-16T14:37:57.515-07:00</updated><title type='text'>Treat class design as type design</title><content type='html'>&lt;span class="gmail_quote"&gt;&lt;br&gt;&lt;/span&gt;   &lt;font face="Arial" size="2"&gt;Class designer = type designer.&lt;/font&gt;&lt;div&gt;&lt;span class="q"&gt;&lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;How should objects fo your new type be created and destroyed ? --this effects your constructors, destructors, memory allocation and deallocation( operator new, new[], delete , delete[]) etc. Shud people call a static fn / normal creation/singleton/ restricted access (??) of some kind , who is responsible for releasing allocated resources, clients or class is self managing, centralized resource release, reference counting or auto ptr semantics when things are copied ? &lt;/font&gt;&lt;/p&gt;&lt;/span&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;How should object initialization differ from object assignment ? -- this answers the differences that will be there between your constructor and your assignment operators. think reference counting, no assignment, deep copy etc ?? &lt;br&gt;&lt;/font&gt;&lt;/p&gt;&lt;span class="q"&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;What does it mean for objects of your new type to be passed by value&amp;nbsp; -- remember pass by value is defined by the implementation of your copy constructor.&lt;/font&gt;&lt;/p&gt;&lt;/span&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;What are the restrictions on legal values for your new type : --- this determines the invariants that your class needs to maintain, invariants determine your error checking and exceptions your functions throw and exception specifications of your functions &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Does your new type fit into an inheritance graph ---if you are inheriting than you are constrained by your parent classes ( their virtual,abstract and non virtual functions and destructors) and if other classes are going to inherit from you than do you want to have virtual/abstract/non virtual functions and destructors ? &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;What kind of type conversion are allowed for your new type --- do you want to define any implicit conversion operators. And explicit conversion functions&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;What operators and functions make sense for the new type --- what functions you want to declare, shud they be member /friend/outside/ in same name space/public functions/private functions/ etc. Does it make sense to nest your class inside another class &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;What standard functions should be disallowed -- declare these private, e.g. may copy in network socket connection management class etc)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;What is the undeclared interface of your new type ---what guarantees it offers w.r.t performance, exception safety, resource usage ( locks , dynamic memory), these impose restraints on your implementation. &lt;/font&gt;&lt;/p&gt;&lt;span class="q"&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;How general is your new type : are you defining a new type or a whole new family of types, if you defined a family of types than you want to define a new class template not just a class&lt;/font&gt;  &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Is a new type really what you need ---- if purpose of new class is just old class + more functionality…then you can maybe use some non member functions or templates to get the work done&lt;/font&gt;  &lt;/p&gt; &lt;br&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Its hard to design smart classes..but worth it.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Things to remember:&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Class design is type design. Before defining a new type , be sure to consider all the issues discussed in this item&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;I need to reread this item and get a hang on it &lt;/font&gt; &lt;/p&gt;  &lt;/span&gt;&lt;/div&gt; &lt;br clear="all"&gt;&lt;br&gt; &lt;br clear="all"&gt;&lt;br&gt;-- &lt;br&gt;Regards,&lt;br&gt;Umesh Kumar&lt;br&gt;&lt;br&gt;&lt;a href="http://worthyarticles.blogspot.com/"&gt;http://worthyarticles.blogspot.com/&lt;/a&gt;&lt;br&gt;&lt;a href="http://learnbooks.blogspot.com/"&gt;http://learnbooks.blogspot.com/ &lt;/a&gt;&lt;br&gt;&lt;a href="http://cplusplusnotes.blogspot.com/"&gt;http://cplusplusnotes.blogspot.com/&lt;/a&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-7462648531657768646?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/7462648531657768646/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=7462648531657768646' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7462648531657768646'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7462648531657768646'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/treat-class-design-as-type-design.html' title='Treat class design as type design'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-2564502796170161872</id><published>2007-06-16T05:09:00.001-07:00</published><updated>2007-06-16T05:09:36.726-07:00</updated><title type='text'>Make interfaces easy to use correctly and hard to use incorrectly</title><content type='html'>&lt;font face="Arial" size="2"&gt;Type system is your primary ally in preventing undesirable code from compiling&lt;/font&gt;&lt;div&gt;&lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Try that either it wont compile, or if it compiles it wont execute properly.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;For e.g. &lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Date( int day , int month . Int year)&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Date ( 3 , 40 , 2001);&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Date( 30 , 3 , 2001);&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Use type system:&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Struct day { explicit(day) } ; struct month { explicit(month);} ; struct year(expli….}&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Date ( day(23) , nmonth…..)&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Be judicious it has become somewhat harder to write now.&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;To make sure you cant do Month(13)…we can&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Have month as an enum type limited to 12, or&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Class month{&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Static month(jan()) { return month(1);&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;…&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;..&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Private month(int m);&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;};&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Another way to contrain things is as shown in item 4…make them const&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Unless there is a good reason your types should behave consistently with built in types&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Std::tr1_shared_ptr&amp;lt;investment&amp;gt; pinv( 0 , getridofinvestment) --wont compile because tr1 needs a explicit ptr, hence&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Std::tr1_shared_ptr&amp;lt;investment&amp;gt; pinv (static_cast&amp;lt;investment*&amp;gt;(0), getridofinvestment() );&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Tr1 avoids the cross dll problem ( what is cross dll problem ?) since by default it uses the version of delete defined in its dll&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Things to remember:&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Good interfaces are easy to use correctly and hard to use incorrectly. You should strive for these characteristics in all your interfaces&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Ways to facilitate correctness use include consistenc in interfaces and behavioral compatiblity with built in types&lt;/font&gt;  &lt;br&gt;&lt;font face="Arial" size="2"&gt;Ways to prevent errors include creating new types , restricting oeprations on types, constraining object values and eliminating client resource management responsiblities&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Arial" size="2"&gt;Tr1::shared_ptr supports custom deleters. This prevenets the cross dll problem , can be used&amp;nbsp; to automatically unlock mutexes etc.&lt;/font&gt;&lt;/p&gt;  &lt;/div&gt; &lt;br clear="all"&gt;&lt;br&gt;-- &lt;br&gt;Regards,&lt;br&gt;Umesh Kumar&lt;br&gt;&lt;br&gt;&lt;a href="http://worthyarticles.blogspot.com/"&gt;http://worthyarticles.blogspot.com/&lt;/a&gt;&lt;br&gt;&lt;a href="http://learnbooks.blogspot.com/"&gt;http://learnbooks.blogspot.com/ &lt;/a&gt;&lt;br&gt;&lt;a href="http://cplusplusnotes.blogspot.com/"&gt;http://cplusplusnotes.blogspot.com/&lt;/a&gt; &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-2564502796170161872?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/2564502796170161872/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=2564502796170161872' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/2564502796170161872'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/2564502796170161872'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/make-interfaces-easy-to-use-correctly.html' title='Make interfaces easy to use correctly and hard to use incorrectly'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-3868599956600384575</id><published>2007-06-14T18:40:00.000-07:00</published><updated>2007-06-14T18:47:55.918-07:00</updated><title type='text'>store newed objects in smart pointers in standalone stmts</title><content type='html'>take an e.g.&lt;br /&gt;&lt;br /&gt;int priority();&lt;br /&gt;void processWidget( std::tr1::shared_ptr&lt;widget&gt; pw, int priority);&lt;br /&gt;&lt;br /&gt;processWIdget( new widget() , priority() )&lt;br /&gt;this wont compile because tr1 constructor in explicit&lt;br /&gt;&lt;br /&gt;so how about&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;processwidget( std::Tr1:;shared_ptr&lt;widget&gt;( new widget) , priority());&lt;br /&gt;&lt;br /&gt;this can cause a memory leak, say compiler decides to evaluate in this order:&lt;br /&gt;&lt;br /&gt;new widget()&lt;br /&gt;priority()&lt;br /&gt;tr1::shared ptr ( widget)&lt;br /&gt;&lt;br /&gt;if priority threw an exception then new widget wud be lost &lt;br /&gt;&lt;br /&gt;the reason of this leak is that their is a lag between the time a resource is created and the time it is turned over to a resource managing object.&lt;br /&gt;way out is simple:&lt;br /&gt;&lt;br /&gt;std::tr1.sharedptr&lt;widget&gt; pw( new widget);&lt;br /&gt;processwidget(pw , priority());&lt;br /&gt;&lt;br /&gt;this works because compiler get less leeway in reordering operations across stmts that within them&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;store newed objects i nsmart pointers in standalone stmts. failure to do this can lead to subtle resource leaks when exceptions are thrown.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-3868599956600384575?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/3868599956600384575/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=3868599956600384575' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3868599956600384575'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3868599956600384575'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/store-newed-objects-in-smart-pointers.html' title='store newed objects in smart pointers in standalone stmts'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-7641990137564867737</id><published>2007-06-14T18:17:00.000-07:00</published><updated>2007-06-14T18:40:04.048-07:00</updated><title type='text'>use the same form in corresponding uses of new and delete</title><content type='html'>match every new with the right delete.&lt;br /&gt;&lt;br /&gt;new -&gt; allocate memory + call constructor on that memory.&lt;br /&gt;delete -&gt; call destructor  + deallocate memory&lt;br /&gt;&lt;br /&gt;the big question for delete is how many objects reside in the memory being deleted . that determines how many destructor's wud be called.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;memory layout for single objects is different from memory layout for arrays of objects.&lt;br /&gt;fore.g. one way is &lt;br /&gt;single objects : ob&lt;br /&gt;array : n , obj , obj ...&lt;br /&gt;&lt;br /&gt;compilers can implement in differently&lt;br /&gt;dependign on the version of delete called it knows whether to delete an array or delete an object.&lt;br /&gt;&lt;br /&gt;remember it when you have multiple constructors, in that case use consistently same  new to allocate memory so that you know what version of delete to call in memory.&lt;br /&gt;&lt;br /&gt;also be careful with typedef's as the author needs to define which delete shud be used with it.&lt;br /&gt;typedef std::string addresslines[4];&lt;br /&gt;std::string *pal = new AddressLines;&lt;br /&gt;&lt;br /&gt;delete pal;&lt;br /&gt;delete[] pal;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;if you use [] in a new expression , you must use [] in the corresponding delete expression. if you dont use [] in a new expression , you mustnt use [] in the corresponding delete expression&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-7641990137564867737?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/7641990137564867737/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=7641990137564867737' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7641990137564867737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7641990137564867737'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/use-same-form-in-corresponding-uses-of.html' title='use the same form in corresponding uses of new and delete'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-400570410640361171</id><published>2007-06-14T18:00:00.000-07:00</published><updated>2007-06-14T18:16:33.413-07:00</updated><title type='text'>provide access to raw resources in resource managing classes</title><content type='html'>you need direct access to raw resource for legacy or other reasons.&lt;br /&gt;&lt;br /&gt;so you wanna provide implicit or explicit conversions to get to the raw resource.&lt;br /&gt;&lt;br /&gt;tr1 and auto both offer a member function to perform explicit conversions  ( interesting what wud happen if i were to take a copy of the raw pointer inside the auto _ptr...do i need to make sure that it wud never get deleted ?)&lt;br /&gt;&lt;br /&gt;tr1 and auto also overload the -&gt; and * operations to allow implicit conversion to the underlying raw pointers&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;you can give an operator to return the original datamember, so that compiler can automatically take the raw pointer when required based on acutal arguments. e.g.&lt;br /&gt;&lt;br /&gt;class Font{&lt;br /&gt;public: operator FontHandle() const { return f ; }&lt;br /&gt;FontHandle f;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;where font is a reosurce manager class for the FontHandle object&lt;br /&gt;&lt;br /&gt;note it increases the chance of an error.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;make interfaces easy to use correctly and hard to use incorrectly&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;raii classes dont exist for resource encapsulation purpose but to ensure that resources are released. so giving out the raw pointer from them isnt really against encapsulation.&lt;br /&gt;&lt;br /&gt;tr1 hides the entire rcsp mechanism ..thus offering clients only what they need to see...so they kind of implement encapsulation of implementation too.&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;apis often require access to raw resource, so eahc raii class should offer a way to get at the resource it manages&lt;br /&gt;access may be via explicit conversion or implicit conversion. in general explicit conversion is safer, but implicit conversion is more convenient for clients.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-400570410640361171?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/400570410640361171/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=400570410640361171' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/400570410640361171'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/400570410640361171'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/provide-access-to-raw-resources-in.html' title='provide access to raw resources in resource managing classes'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-8553818320498379908</id><published>2007-06-14T17:42:00.000-07:00</published><updated>2007-06-14T18:00:21.006-07:00</updated><title type='text'>think carefully about copying behaviour in resource managing classes</title><content type='html'>for non heap based resources smart pointers like auto_ptr and tr1:;shread_ptr are generally inappropriate as resource handlers. you can just create your own resource managing classes for this purpose.&lt;br /&gt;&lt;br /&gt;for e.g. mutex handler class, that acquires mutex while it is initializing in the constructor and releases the mutex in the destructor of the class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;now you need to worry about the copying behavior of this classes &lt;br /&gt;&lt;br /&gt;general question we discuss is what happens when a raii class is copied. some options are :&lt;br /&gt;prohibit copying: does it makes sense to allow copying of the object.( see item 6 on how to do it)&lt;br /&gt;&lt;br /&gt;reference count the underlying source: if you want to hold on to a resource  until the last object using it has been destroyed....similar copy meaning as in tr1..shared..in this case u can implement it using a data member of tr1..shared&lt;br /&gt;&lt;br /&gt;talking of mutex for second case....tr1 will delete it by default ( though we want to release lock when reference count goes to zero)... hey you can specify a deleter function for tr1::shared_ptr....its not available for auto_ptr though&lt;br /&gt;&lt;br /&gt;class lock {&lt;br /&gt;public:&lt;br /&gt; explicit Lock(mutex* pm) : mutexPtr(pm, unlock) { lock(mutexptr.get() }&lt;br /&gt;private : &lt;br /&gt; std::tr1_shared_ptr&lt;mutex&gt; mutexPtr;&lt;br /&gt;};&lt;br /&gt;note that unlock is the deleter function here&lt;br /&gt;&lt;br /&gt;note that when count will go to zero the mutex wud automatically get deleted....lock destroyed-&gt;tr1 destructor -&gt; unlock function&lt;br /&gt;&lt;br /&gt;Third way out is : copy the underlying resource:&lt;br /&gt;if having multiple copies is okay and you choose resource managing class so that any allocated resource is always freed ...u shud be a doing a deep copy.&lt;br /&gt;&lt;br /&gt;fourth option is : transfer ownership of the underlying resource...like auto_ptrs.&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;copying an raii objects entails copying the resource it manages, so the copying behavior of the resource determines the copying behavior of the raii object&lt;br /&gt;&lt;br /&gt;common raii class copying behaviors are disallowing copying and performing reference counting but other behaviors are possible.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-8553818320498379908?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/8553818320498379908/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=8553818320498379908' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/8553818320498379908'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/8553818320498379908'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/think-carefully-about-copying-behaviour.html' title='think carefully about copying behaviour in resource managing classes'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-6749801036876175277</id><published>2007-06-13T19:13:00.000-07:00</published><updated>2007-06-13T19:35:05.196-07:00</updated><title type='text'>use objects to manage resources</title><content type='html'>make sure that any resource allocated is always released ( think network connections, database connections, file handlers, lock, mutex, memory, other things)&lt;br /&gt;&lt;br /&gt;mistakes are easy:&lt;br /&gt;//createInvestment is a factory function that returns a investment object&lt;br /&gt;int foo()&lt;br /&gt;{&lt;br /&gt; Investment * prt = createInvestment();&lt;br /&gt;  complex code that might return somehow ( think if /else/exception throw/code changed by someone/ some function that it called was "enhanced")&lt;br /&gt;&lt;br /&gt;   delete prt&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;so always use an object to manager resources.&lt;br /&gt;&lt;br /&gt;STL auto_ptr is a good thing to use when resource is only used within a single block or function only.&lt;br /&gt;e.g&lt;br /&gt;&lt;br /&gt;int foo()&lt;br /&gt;{&lt;br /&gt; std::auto_ptr&lt;Investment&gt;  prt( createInvestment());&lt;br /&gt; ......&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Two critical aspects:&lt;br /&gt;Resource are acquired and immediately turned over to resource managing objects&lt;br /&gt;resource managing objects use their destructors to make sure that resources are released ....since destructor wud be called when control leaves the block, so resources are automatically released. this can get tricky with exceptions being thrown in destructor's( something u shud never do..item 8) &lt;br /&gt;&lt;br /&gt;coming back to auto_ptrs&lt;br /&gt;auto_ptrs automatically delete whatever they were pointing to at the end&lt;br /&gt;so when u copy a auto_ptr into another the first one looses its value, for e.g.&lt;br /&gt;&lt;br /&gt;auto_ptr &lt; inv..&gt; * ptr(new...)&lt;br /&gt;ptr2 = ptr1;&lt;br /&gt;  ///ptr1 is null here&lt;br /&gt;&lt;br /&gt;so you cant have auto_ptrs containers ( vector/list etc of auto_ptrs) since stl containers expect normal copying behavior&lt;br /&gt;&lt;br /&gt;Another alternative to auto_ptrs is Reference counting smart pointer.&lt;br /&gt;How does RCSP's differ from garbage collection -- cant break cycles of refererences&lt;br /&gt;&lt;br /&gt;these can be used in stl containers&lt;br /&gt;&lt;br /&gt;their is tr1:shared_ptr also which gives you RCSP behavior&lt;br /&gt;&lt;br /&gt;Note that both tr1 and auto use delete in their implementation so so you cannt pass them arrays ( vector etc are good)&lt;br /&gt;&lt;br /&gt;e.g. &lt;br /&gt;std::auto_ptr&lt;std::string&gt; aps(new std::string[10]); this is bad since delete wud be called and not delte[] which is the right destructor to call.&lt;br /&gt;&lt;br /&gt;So basically if you are releasing resources manually then you have done something wrong.&lt;br /&gt;&lt;br /&gt;mostly auto or tr1 can help you out.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;to prevent resource leaks, use RAII objects tha acquire resources in their constructors and release them in their destructors&lt;br /&gt;two commonly useful RAII classes are tr1::shared_ptr and auto_ptr tr1::shared_ptr is usually the better choice , because its behavior when copied is intuitive.,  copying an auto_ptr sets it to null&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-6749801036876175277?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/6749801036876175277/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=6749801036876175277' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/6749801036876175277'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/6749801036876175277'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/use-objects-to-manage-resources.html' title='use objects to manage resources'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-5532464374706656358</id><published>2007-06-10T11:20:00.000-07:00</published><updated>2007-06-10T11:29:30.276-07:00</updated><title type='text'>copy all parts of an object</title><content type='html'>copying functions = copy constructor, copy assignment&lt;br /&gt;&lt;br /&gt;when you add any data member or something else, make sure that all the copying functions are properly modified for this.&lt;br /&gt;&lt;br /&gt;in a good design only copy constructor and copy assignment are the only functiosn copying objects&lt;br /&gt;&lt;br /&gt;make sure derived classes copy all the elements of their base class also.&lt;br /&gt;&lt;br /&gt;why shud we not call one copying funtion from the other ?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;copying functions should be sure to copy all of an objects data members and all of its base class parts&lt;br /&gt;&lt;br /&gt;dont try to implement one of the copying functions in terms of the other. insread put common functionality in a third funtion that both call&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-5532464374706656358?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/5532464374706656358/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=5532464374706656358' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5532464374706656358'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/5532464374706656358'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/copy-all-parts-of-object.html' title='copy all parts of an object'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-3661322911900520928</id><published>2007-06-10T11:09:00.000-07:00</published><updated>2007-06-10T11:20:33.285-07:00</updated><title type='text'>item11</title><content type='html'>handle assignments to self in operator =&lt;br /&gt;&lt;br /&gt;self assignment is likely to happen for e.g.&lt;br /&gt;a[i] = a[j] &lt;br /&gt;*pt = *pg&lt;br /&gt;&lt;br /&gt;or some other way it will happen.&lt;br /&gt;&lt;br /&gt;in resource management classes be esp aware of releasing current resources and self assignment causing trouble. imagine&lt;br /&gt;widget&amp; widget::operator=(const widget&amp; rhs_&lt;br /&gt;{&lt;br /&gt; delete pb;&lt;br /&gt; pb = new Bitmap)*rhs.pb);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;u can use if( this = &amp;rhs)  then return at top of function for this.&lt;br /&gt;&lt;br /&gt;also the above is exception unsafe....pb will point to a deleted object if new Bitmap throws exception&lt;br /&gt;&lt;br /&gt;so use combination of save elsewhere, copy new, delete elsewhere to avoid exception ....that makes it self assignment safe too&lt;br /&gt;&lt;br /&gt;one thing is if someone else kept a ptr to this pb then self assignment has caused a complete copy to happen...also the new address is different from older address ( old pb[2] isnt same as new pb[2]) ....does this make sense ?&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;make sure operator= is well behaved when an object is assigned to itself techniques include comparing addresses of source and target objects, careful stmt ordering and copy and swap&lt;br /&gt;&lt;br /&gt;make sure that any function operating on more than one object behaves correctly if two or more of the objects are the same,&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-3661322911900520928?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/3661322911900520928/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=3661322911900520928' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3661322911900520928'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3661322911900520928'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/item11.html' title='item11'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-3531833241744335022</id><published>2007-06-10T08:48:00.000-07:00</published><updated>2007-06-10T08:51:37.540-07:00</updated><title type='text'>item 10</title><content type='html'>have assignment operators return a reference to *this&lt;br /&gt;&lt;br /&gt;in built in types...assignment returns a reference to its left hand operand....so you wanna follow the convention&lt;br /&gt;&lt;br /&gt;it applies to all operators not just assignment&lt;br /&gt;&lt;br /&gt;class widget {&lt;br /&gt;public:&lt;br /&gt; widget&amp; operator+=(const widget&amp; rhs)&lt;br /&gt;{...&lt;br /&gt; return *this);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;widget&amp; operator=(int rhs)&lt;br /&gt;{&lt;br /&gt; ....&lt;br /&gt;return *this ;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;unless you have a good reason for doing things differently, dont.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;have assignement operator a reference to *this&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-3531833241744335022?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/3531833241744335022/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=3531833241744335022' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3531833241744335022'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/3531833241744335022'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/item-10.html' title='item 10'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-4608106160720652222</id><published>2007-06-10T07:49:00.000-07:00</published><updated>2007-06-10T08:48:01.337-07:00</updated><title type='text'>item 9</title><content type='html'>n&lt;br /&gt;ver call virtual functions during construction or destruction&lt;br /&gt;&lt;br /&gt;during base class construction virtual functions never go down into derived classes.&lt;br /&gt;&lt;br /&gt;at what exact time does the derived class object come into existence ? at the momwnt the curly brace of constructor is entered ? or the moment the ) of arguments is seen ?&lt;br /&gt;&lt;br /&gt;and similarly at what point is the derived object destructed ?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If you find that you need a virtual function call in the base class than you should have the dervied class pass the necessary info down to the base class and the non virtual function in base class can do the handling for you.&lt;br /&gt;&lt;br /&gt;But make sure that thru no path of invocation, a virtual function is getting invoked till the constructor finishes ( or till some other point)? ( think insidiously...constructor calls non virtual function which calls a virtual function)...you need to make sure that this doesnt happen.&lt;br /&gt;&lt;br /&gt;i think this is a big dilemma....if constructor cant call a virtual function then how do you provide extensiblity in the base class for things that need to be done during construction itself ?&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;dont call virtual functions during construction or destruction. because such calls will never go to a more dervied class than that of the currently executing constructor or destructor.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-4608106160720652222?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/4608106160720652222/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=4608106160720652222' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/4608106160720652222'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/4608106160720652222'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/item-9.html' title='item 9'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-1147965989678745057</id><published>2007-06-09T18:51:00.000-07:00</published><updated>2007-06-09T18:58:28.006-07:00</updated><title type='text'>item 8</title><content type='html'>prevent exceptions from leaving destructors.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;not prohibited in c++, but dont throw them,&lt;br /&gt;&lt;br /&gt;imagine a vector&lt;widget&gt; v having 10 elements and v[1] destructor ( on destruction of v) throws an exception.&lt;br /&gt;&lt;br /&gt;if your destructor cant avoid having to throw an exception ( imagine destructor of a class providing db connections--resource managing classes) than code it to either terminate the program or just swallow the exception whichever is suitable.  Swallowing is considered bas compared to termination.&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;destructors should never throw exceptions. if functions called in a destructor may throw, the destructor should catch any exceptions then tswallow them or terminate the program&lt;br /&gt;&lt;br /&gt;if class clients need to be able to react to exceptions thrown during an operation the class should provide a refular ( i.e. no ndestructor) function that performs the operation&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-1147965989678745057?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/1147965989678745057/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=1147965989678745057' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/1147965989678745057'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/1147965989678745057'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/item-8.html' title='item 8'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-6496007732691627460</id><published>2007-06-09T18:40:00.000-07:00</published><updated>2007-06-09T18:50:42.341-07:00</updated><title type='text'>item 7</title><content type='html'>declare destructors virtual in polymorphic base classes&lt;br /&gt;&lt;br /&gt;a factory function is a function that returns a base class pointer to a newly created derived class object.&lt;br /&gt;&lt;br /&gt;to make sure that when a derived class object is deleted from a base class pointer than the entire object is deleted and not only the base class part of the object ( which is undefined....c++ doesnt define delete semantics when a base class destructor is called on a derived class object).&lt;br /&gt;&lt;br /&gt;any class with virtual functions should almost certainly have a virtual constructor.&lt;br /&gt;&lt;br /&gt;if a class doesnt have virtual fns than dont make the destructor virtual.&lt;br /&gt;&lt;br /&gt;u can declare a pure virtual destructor to make a class abstract&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;plymorphic base classes should declare virtual destructors. if a class has virtual functions, it should have a virtual destructor.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;classes not designd to be base classes or not designed to be used ploymorphically should not declare virtual destructors&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-6496007732691627460?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/6496007732691627460/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=6496007732691627460' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/6496007732691627460'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/6496007732691627460'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/item-7.html' title='item 7'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-8789022139186548469</id><published>2007-06-06T21:13:00.003-07:00</published><updated>2007-06-06T21:13:43.684-07:00</updated><title type='text'>C++ item 6</title><content type='html'>&lt;br&gt;&lt;span class="gmail_quote"&gt;&lt;br&gt;&lt;br&gt;&lt;/span&gt; &lt;div&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;All compiler generated functions are public&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;To make sure that none can call a function foo() in your class.&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Declare it as a private foo functions.&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;And don't define it.&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Then even if friend or member functions were to invoke it then you will get a linker error.&lt;/font&gt; &lt;/p&gt;&lt;br&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;A better way to get this linker error at compile time is : &lt;/font&gt;&lt;br&gt;&lt;font face="Arial" size="2"&gt;Create a uncopyable class with cons , des, copy constructror et.c.&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Now have general_class privately inherit from pupcopyabele class&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Things to remember:&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;To disallow functionality automatically provided by compilers. Declare the corresponding member functions private and give no implementation. Using a base class like uncopyable is one wat to do this &lt;/font&gt;&lt;/p&gt;&lt;/div&gt;&lt;br clear="all"&gt;&lt;br&gt;-- &lt;br&gt;Regards,&lt;br&gt;Umesh Kumar&lt;br&gt;&lt;br&gt;&lt;a href="http://worthyarticles.blogspot.com/"&gt;http://worthyarticles.blogspot.com/&lt;/a&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-8789022139186548469?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/8789022139186548469/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=8789022139186548469' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/8789022139186548469'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/8789022139186548469'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/c-item-6.html' title='C++ item 6'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-7361894262417623933</id><published>2007-06-06T21:13:00.001-07:00</published><updated>2007-06-06T21:13:21.532-07:00</updated><title type='text'>C++ item 5</title><content type='html'>&lt;br&gt;&lt;br&gt;&lt;span class="gmail_quote"&gt;&lt;br&gt;&lt;br&gt;&lt;/span&gt; &lt;div&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;An empty class isnt really empty.&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;A compilergenerates definition for these functions automatically.&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Default constructor if u don't have a constructor&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Copy constructor &lt;/font&gt;&lt;br&gt;&lt;font face="Arial" size="2"&gt;Copy assignemtn &lt;/font&gt;&lt;br&gt;&lt;font face="Arial" size="2"&gt;Destructor&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Class Empty {&lt;/font&gt; &lt;br&gt;&lt;font face="Arial" size="2"&gt;Public:&lt;/font&gt; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font face="Arial" size="2"&gt;Empty() {}&lt;/font&gt; &lt;br&gt;&lt;font face="Arial" size="2"&gt;Empty( const Empty&amp;amp; rhs) { } &lt;/font&gt; &lt;br&gt;&lt;font face="Arial" size="2"&gt;~Empty() { }&lt;/font&gt; &lt;br&gt;&lt;font face="Arial" size="2"&gt;Empty&amp;amp; operator=(const Empty&amp;amp; rhs) { } &lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;};&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;These are generated only if needed, but are esily needed for .e.g&lt;/font&gt; &lt;br&gt;&lt;font face="Arial" size="2"&gt;Empty e1 &lt;/font&gt;&lt;br&gt;&lt;font face="Arial" size="2"&gt;Empty e2(e1)&lt;/font&gt; &lt;br&gt;&lt;font face="Arial" size="2"&gt; E2 = e1&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Causes generation of all of them&lt;/font&gt; &lt;/p&gt;&lt;br&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;What does it mean : compilers reject implicit copy assignment operators in derived classess that inherit from base classes declaring the copy assignment operator private. ?&lt;/font&gt;&lt;/p&gt;&lt;br&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Things to remember:&lt;/font&gt; &lt;/p&gt; &lt;p&gt;&lt;font face="Arial" size="2"&gt;Compilers may implicitly generate a class&amp;#39;s default constructor , copy constructor , copy assignment operator and destructor&lt;/font&gt;&lt;/p&gt;&lt;/div&gt;&lt;br clear="all"&gt;&lt;br&gt;-- &lt;br&gt;Regards,&lt;br&gt;Umesh Kumar &lt;br&gt;&lt;br&gt;&lt;a href="http://worthyarticles.blogspot.com/"&gt;http://worthyarticles.blogspot.com/&lt;/a&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-7361894262417623933?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/7361894262417623933/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=7361894262417623933' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7361894262417623933'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7361894262417623933'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/c-item-5.html' title='C++ item 5'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-7075303187502328494</id><published>2007-06-03T18:58:00.001-07:00</published><updated>2007-06-03T18:58:36.904-07:00</updated><title type='text'>Item 4</title><content type='html'>make sure the objects are initialized before they are used:&lt;br /&gt;&lt;br /&gt;in general if you are in c part of c++ and initialization wud incur a runtime cost then it is not guaranteed to take place ( actual rules are complex).&lt;br /&gt;&lt;br /&gt;always initialize your objects&lt;br /&gt;&lt;br /&gt;differentiate between initialization and assignment.&lt;br /&gt;&lt;br /&gt;for built in types their is no difference between initialization and assignment&lt;br /&gt;&lt;br /&gt;the relative order of initialization of non local static objects defined in different translation units is undefined.&lt;br /&gt;&lt;br /&gt;it is so because determining the correct order is unsolvably hard.&lt;br /&gt;&lt;br /&gt;you can use static functions to get around this problem. but they are a problem in multithreaded functions.&lt;br /&gt;&lt;br /&gt;what does it mean when you say " any kind of non const static object local or non local is trouble waiting to happen in the presence of multiple threads " ?&lt;br /&gt;&lt;br /&gt;the easiest way around is to invoke them in proper order in the single threaded startup process.&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;manually initialise objects of built in type because c++ only sometimes initializes them itself.&lt;br /&gt;in a constructor prefer use of the member initialization list to assignment inside the body of the constructor. list data members in the initialization list in the same order they are declared in the class.&lt;br /&gt;avaoid initialization order problems across translation units be replacing non local static objects with local static objects.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-7075303187502328494?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/7075303187502328494/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=7075303187502328494' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7075303187502328494'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7075303187502328494'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/item-4.html' title='Item 4'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-4787997835384439501</id><published>2007-06-03T18:57:00.002-07:00</published><updated>2007-06-03T18:58:04.239-07:00</updated><title type='text'>Item 3</title><content type='html'>use const whenerver possible:&lt;br /&gt;&lt;br /&gt;const allows you to specify a semantic constraint and compilers enforce it for you.&lt;br /&gt;&lt;br /&gt;if the word const appears to the left of the asterisk what is pointed to is constant&lt;br /&gt;if it is on left than the pointer itself is constant.&lt;br /&gt;&lt;br /&gt;const widget* ptr = widget const * prt&lt;br /&gt;&lt;br /&gt;having a function return a constant value often makes it possible to reduce the incidence of client errors without giving up the safety or efficiency&lt;br /&gt;&lt;br /&gt;e.g.&lt;br /&gt;class Rational {...}&lt;br /&gt;const Rational operator* (const Rational&amp; a , const Rational &amp; b);&lt;br /&gt;&lt;br /&gt;this causes a compile error if clients were to say ?&lt;br /&gt;&lt;br /&gt;Rational a ,b,cl&lt;br /&gt;( a*b) = c ;&lt;br /&gt;&lt;br /&gt;the above can be caused by a simple type in a comparison statement&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;one of the hallmarks of good user defined types is that they avoid gratuitous incompatiblities with the built ins.&lt;br /&gt;&lt;br /&gt;const member functions make interface easier to understand as you know whether a given function can change the object or not.&lt;br /&gt;They help efficiency as you can work with const&amp; which are a way to improve efficiency in function calls taking uder objects as parameters.&lt;br /&gt;&lt;br /&gt;functions differing only in their constness can be overloaded.e.g.&lt;br /&gt;&lt;br /&gt;class TextBlock {&lt;br /&gt;public:&lt;br /&gt;const char&amp; operator[] ( std::size_t position) const { return text[position];}&lt;br /&gt;char&amp; operator[] ( std::size_t position) const { return text[position];}&lt;br /&gt;&lt;br /&gt;private std::string text;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;TextBlock tb;&lt;br /&gt;std::cout &lt;&lt; tb[0]; ---second function called&lt;br /&gt;&lt;br /&gt;const TextBlock ctb;&lt;br /&gt;std::cout &lt;&lt; ctb[0]; ---first function called&lt;br /&gt;&lt;br /&gt;note that we need to return references otherwise constness doesnt make sense. since the retunr value will get copied and const becomes useless.&lt;br /&gt;&lt;br /&gt;will the below compile ?&lt;br /&gt;&lt;br /&gt;class TextBlock {&lt;br /&gt;public:&lt;br /&gt;const char operator[] ( std::size_t position) const { return text[position];}&lt;br /&gt;char operator[] ( std::size_t position) const { return text[position];}&lt;br /&gt;&lt;br /&gt;private std::string text;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;constness = physical constness / logical constness&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;it says that this code compiles, whereas i think i have got an error when i tried to compile it in past:&lt;br /&gt;&lt;br /&gt;class CTextBlock {&lt;br /&gt;public:&lt;br /&gt;char&amp; operator[](std::size_t position) const&lt;br /&gt;{ return pText[position]; }&lt;br /&gt;private char* pText;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;mutable frees the non static data members from the constraints of bitwise constness&lt;br /&gt;&lt;br /&gt;avoid code duplication in const and non const member functions:&lt;br /&gt;see if you can call the const version from the non const version and cast away the constness ( dont do other way round....since the non const function change something in the object passed to it)&lt;br /&gt;&lt;br /&gt;---also make sure you dont get yourself into an infinite loop non const calling itself again and again&lt;br /&gt;&lt;br /&gt;things to remember:&lt;br /&gt;declaring something const helps compiler detect usage errors. const can be applied to objects at any scope, to function parameters and return types and to member functions as a whole.&lt;br /&gt;compilers enforce bitwise constness, but you should program using conceptual constness&lt;br /&gt;when const and non const member functions have essentially identical implementations code duplications can be avoided by having the non const version call the const version&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-4787997835384439501?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/4787997835384439501/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=4787997835384439501' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/4787997835384439501'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/4787997835384439501'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/item-3.html' title='Item 3'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-2557762398164358772</id><published>2007-06-03T18:57:00.001-07:00</published><updated>2007-06-03T18:57:32.333-07:00</updated><title type='text'>Item 2</title><content type='html'>prefer consts enums and inline to #define&lt;br /&gt;&lt;br /&gt;strings are preferable to const char*&lt;br /&gt;&lt;br /&gt;so const std::string author("scott meyers");&lt;br /&gt;&lt;br /&gt;is preferable to&lt;br /&gt;const char * const author = "scott meyers"&lt;br /&gt;&lt;br /&gt;class gamePlayer&lt;br /&gt;{&lt;br /&gt;private:&lt;br /&gt;static const int numTurns = 5;&lt;br /&gt;int scores[numTurns];&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;above is just a declaration for numTurns and not a definition.&lt;br /&gt;&lt;br /&gt;c++ allows u to use after declaring only if its a static int and its address isnt taken.&lt;br /&gt;&lt;br /&gt;if you take address of a class, then you have to define it also:&lt;br /&gt;&lt;br /&gt;const int GamePlayer::numTurns;--- this goes in impl file not header file&lt;br /&gt;&lt;br /&gt;in class initialization is allowed only for const ints.&lt;br /&gt;&lt;br /&gt;so&lt;br /&gt;&lt;br /&gt;const costEstimate&lt;br /&gt;{&lt;br /&gt;private:&lt;br /&gt;static const double fudgeFactor ;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;const double constEstimate::fudgeFactor = 1.35;&lt;br /&gt;&lt;br /&gt;how to get the value if you need it for compilation, as in the first class where you needed it to create the array: ( some compilers dont allow declaring value inside the class so you cannt use style of 1 )&lt;br /&gt;&lt;br /&gt;class gamePlayer{&lt;br /&gt;private:&lt;br /&gt;enum { numTurns = 5};&lt;br /&gt;int scores[numTurns];&lt;br /&gt;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;it is known as the enum hack.&lt;br /&gt;&lt;br /&gt;it allows you to make sure people cannt get a reference to your constants.&lt;br /&gt;it also makes sure compilers wont allocate any memory for it.&lt;br /&gt;&lt;br /&gt;can people access that value or you can just declare a private enum ?&lt;br /&gt;&lt;br /&gt;why wud the value not get picked up if it is cost Estimate style ?&lt;br /&gt;&lt;br /&gt;try not to use macros at all. you can get all the efficiency of a macro with the type safety of a regular function by using templates for an inline functions.&lt;br /&gt;&lt;br /&gt;e.g.&lt;br /&gt;#define call_with_max(a,b) f((a) &gt;(b) ? (a) : (b) )&lt;br /&gt;&lt;br /&gt;replace with&lt;br /&gt;&lt;br /&gt;template&lt;br /&gt;inline void callWithMax( const T&amp;a , cons t T&amp;b )&lt;br /&gt;{&lt;br /&gt;f ( a&gt; b ? a:b);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;preprocessor is bad but we need #include and #ifndef and #define&lt;br /&gt;things to remember from this article:&lt;br /&gt;&lt;br /&gt;for simple constants prefer const objects to enums to #defines&lt;br /&gt;for function like macros prefer inline functions to #defines&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-2557762398164358772?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/2557762398164358772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=2557762398164358772' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/2557762398164358772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/2557762398164358772'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/item-2.html' title='Item 2'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-635788831787428145.post-7841090597193972643</id><published>2007-06-03T18:56:00.000-07:00</published><updated>2007-06-03T18:57:04.234-07:00</updated><title type='text'>Item 1</title><content type='html'>C++ has four dimensions :&lt;br /&gt;c , object oriented c++ , template c++ , stl&lt;br /&gt;&lt;br /&gt;the rules change when you move from one area to another so you need to know what changes and how.&lt;br /&gt;&lt;br /&gt;to quote " c++ is not a unified language with a set of rules. it is a federation of four languages. C , object oriented c++ , template c++ , stl&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/635788831787428145-7841090597193972643?l=cplusplusnotes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cplusplusnotes.blogspot.com/feeds/7841090597193972643/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=635788831787428145&amp;postID=7841090597193972643' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7841090597193972643'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/635788831787428145/posts/default/7841090597193972643'/><link rel='alternate' type='text/html' href='http://cplusplusnotes.blogspot.com/2007/06/item-1.html' title='Item 1'/><author><name>Umesh</name><uri>http://www.blogger.com/profile/10866274125038489683</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
