<?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-8117812670692388711</id><updated>2011-04-21T22:01:07.663+01:00</updated><category term='Pointers References c++'/><category term='Friend'/><category term='C++'/><category term='Java XML StAX XStream Woodstox comparison'/><category term='Const C++ Learning'/><category term='Inheritance Casting Pointers References C++'/><category term='XML Java'/><category term='Function'/><category term='Class'/><title type='text'>How to become a programmer in 21 years</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://becomeaprogrammerin21years.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://becomeaprogrammerin21years.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Lurning Man</name><uri>http://www.blogger.com/profile/03377787338729172137</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>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8117812670692388711.post-5103263393277800912</id><published>2008-11-05T17:15:00.004Z</published><updated>2008-11-06T17:51:43.922Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java XML StAX XStream Woodstox comparison'/><title type='text'>More Java XML</title><content type='html'>Adding to my previous post about Java XML.&lt;br /&gt;&lt;br /&gt;Take the example where you want to take an existing class and customize your own XML output. You could use XStream out of the box without any configuration and just trust it to create the correct XML. This is fine if you want the object to be fully represented in the XML, but what if you only want a subset of the data in the object?&lt;br /&gt;&lt;br /&gt;Well, XStream allows you to write custom converters for this. So, you need to create a converter class (that implements the Converter interface), and in the marshal() method, take the fields that you desire and manually create the XML nodes. This gives you much more control over the output.&lt;br /&gt;&lt;br /&gt;Another option is to use StAX to directly map the input elements to the output XML. Again this gives you full control as to what is going into the actual XML document.&lt;br /&gt;&lt;br /&gt;I ran a few tests to determine the performance difference between Woodstox (a StAX implementation) and XStream. Basically the test involved both libraries marshalling an object into XML data 100000 times. The difference was quite large, with Woodstox outperforming XStream by about 30~40%.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8117812670692388711-5103263393277800912?l=becomeaprogrammerin21years.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://becomeaprogrammerin21years.blogspot.com/feeds/5103263393277800912/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8117812670692388711&amp;postID=5103263393277800912' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/5103263393277800912'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/5103263393277800912'/><link rel='alternate' type='text/html' href='http://becomeaprogrammerin21years.blogspot.com/2008/11/more-java-xml.html' title='More Java XML'/><author><name>Lurning Man</name><uri>http://www.blogger.com/profile/03058824127760123204</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-8117812670692388711.post-5335043504749149198</id><published>2008-10-31T17:44:00.002Z</published><updated>2008-10-31T18:08:11.655Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='XML Java'/><title type='text'>XML Data Binding and Serialization Java</title><content type='html'>When working with XML data, there are a number of libraries that can be used, depending on the complexity of your needs.&lt;br /&gt;&lt;br /&gt;For a once off reading of an XML file, you can just use a SAX/DOM based parser, such as those defined in the javax.xml.parsers.&lt;br /&gt;&lt;br /&gt;If you want to get a particular element or attrbute out of an XML document, then you can use the javax.xml.xpath library to parse the document. This makes it easy to quickly find a single element, or set of elements in an XML document.&lt;br /&gt;&lt;br /&gt;If you want to do something more heavyweight, and you are going to use a particular XML schema repeatedly, then you will probably need have some sort of binding/marshalling libraries. Binding refers to the action of taking an XML schema and automatically making a set of classes that correspond to the schema. Marshalling goes the opposite way and creates an XML file from a class. Note that to marshall an object, you don't necessarily need to have bound it previously. You can just declare any old class and marshall it.&lt;br /&gt;&lt;br /&gt;The most common binging/marshalling library is JAXB (javax.xml.bind.JAXB). This includes a tool for automatically binding an XML schema to a set of classes, as well as a Marshaller that will create XML from the created objects.&lt;br /&gt;&lt;br /&gt;If you just want to marshall an object, then there are libraries, such as XStream, that make this very simple. Just create any old object, and use XStream to define/create an XML document for it. It is a quick and easy solution, especially if you want to define your own classes to hold the XML data (instead of going down the automatic binding route).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8117812670692388711-5335043504749149198?l=becomeaprogrammerin21years.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://becomeaprogrammerin21years.blogspot.com/feeds/5335043504749149198/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8117812670692388711&amp;postID=5335043504749149198' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/5335043504749149198'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/5335043504749149198'/><link rel='alternate' type='text/html' href='http://becomeaprogrammerin21years.blogspot.com/2008/10/xml-data-binding-and-serialization-java.html' title='XML Data Binding and Serialization Java'/><author><name>Lurning Man</name><uri>http://www.blogger.com/profile/03058824127760123204</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-8117812670692388711.post-1978983269693592994</id><published>2008-03-23T16:36:00.008Z</published><updated>2008-03-24T21:25:44.140Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='Function'/><category scheme='http://www.blogger.com/atom/ns#' term='Class'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='Friend'/><title type='text'>Friends in C++</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Friend&lt;/span&gt; functions and classes are not the most common of C++ features but there are situations where the &lt;span style="font-weight: bold;"&gt;friend&lt;/span&gt; keyword is useful.&lt;br /&gt;&lt;br /&gt;A friend is something that allows your class to be accessed by a different class or function. The &lt;span style="font-weight: bold;"&gt;friend&lt;/span&gt; keyword can be used in 2 cases, to declare a friend function or a friend class.&lt;br /&gt;&lt;br /&gt;When you declare a friend function in your class, then it can do anything that a member function of the class can do. So in the following, &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_0"&gt;friendFn&lt;/span&gt;()&lt;/span&gt; can do anything to the object passed to it that &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_1"&gt;memberFn&lt;/span&gt;()&lt;/span&gt; can do.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp0.blogger.com/_23u4Bwj5BH0/R-gUoXm9ATI/AAAAAAAAACc/P_mttcOaNfA/s1600-h/Friend1.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp0.blogger.com/_23u4Bwj5BH0/R-gUoXm9ATI/AAAAAAAAACc/P_mttcOaNfA/s400/Friend1.JPG" alt="" id="BLOGGER_PHOTO_ID_5181414055054475570" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;At the end of the above example, the private &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_2"&gt;popObject&lt;/span&gt;&lt;/span&gt; data &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_3"&gt;mData&lt;/span&gt;&lt;/span&gt; is 6. Note that the object passed is only changed as it is passed as a reference. If the function declaration for &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_4"&gt;friendFn&lt;/span&gt;&lt;/span&gt; was as follows:&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_5"&gt;friendFn&lt;/span&gt;(&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_6"&gt;PopularClass&lt;/span&gt; &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_7"&gt;pObject&lt;/span&gt;, int x);&lt;br /&gt;&lt;/span&gt;then &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_8"&gt;friendFn&lt;/span&gt;()&lt;/span&gt; would have made a copy of the object passed and changed its member variable.&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Friend Classes follow along the same principle, they allow another class to have access to the data in a class. So in the following, &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_9"&gt;PopularClass&lt;/span&gt;&lt;/span&gt; allows the class &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_10"&gt;FriendClass&lt;/span&gt;&lt;/span&gt; access to its private data. So when &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_11"&gt;fObject&lt;/span&gt;.&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_12"&gt;addVals&lt;/span&gt;(&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_13"&gt;popobject&lt;/span&gt;) &lt;/span&gt;&lt;span&gt;gets its hands on &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_14"&gt;popObject&lt;/span&gt;&lt;/span&gt; it is allowed access to the private data so that it can add the private data from both its own class (which it, of course, always had access to) and the passed in &lt;span style="font-weight: bold;"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_15"&gt;popObject&lt;/span&gt; &lt;/span&gt;which is an object of a different class&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp1.blogger.com/_23u4Bwj5BH0/R-gYjnm9AUI/AAAAAAAAACk/rhxWzSJXyGI/s1600-h/Friend2.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp1.blogger.com/_23u4Bwj5BH0/R-gYjnm9AUI/AAAAAAAAACk/rhxWzSJXyGI/s400/Friend2.JPG" alt="" id="BLOGGER_PHOTO_ID_5181418371496608066" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;So friends can be useful if you treat them right (sorry, way too corny I know...)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8117812670692388711-1978983269693592994?l=becomeaprogrammerin21years.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://becomeaprogrammerin21years.blogspot.com/feeds/1978983269693592994/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8117812670692388711&amp;postID=1978983269693592994' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/1978983269693592994'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/1978983269693592994'/><link rel='alternate' type='text/html' href='http://becomeaprogrammerin21years.blogspot.com/2008/03/friends-in-c.html' title='Friends in C++'/><author><name>Lurning Man</name><uri>http://www.blogger.com/profile/03377787338729172137</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><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp0.blogger.com/_23u4Bwj5BH0/R-gUoXm9ATI/AAAAAAAAACc/P_mttcOaNfA/s72-c/Friend1.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8117812670692388711.post-9185400949636591540</id><published>2008-03-01T17:40:00.006Z</published><updated>2008-03-01T18:56:48.244Z</updated><title type='text'>C++ inheritance oddity</title><content type='html'>There are plenty of things that can make C++ a complicated language. But in general the creators of C++ would have gone though a painstaking process to ensure that the language as intuitive and useable as possible (without sacrificing on functionality of course). However, when I came accross the following C++ behaviour I was confused.&lt;br /&gt;&lt;br /&gt;Lets just say that I have a base class and a derived class. The base class has 3 member functions, each called &lt;span style="font-weight: bold;"&gt;doSomething()&lt;/span&gt; as below.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp1.blogger.com/_23u4Bwj5BH0/R8mZIijgZlI/AAAAAAAAAB8/G_giQU1cp-I/s1600-h/BaseOK.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp1.blogger.com/_23u4Bwj5BH0/R8mZIijgZlI/AAAAAAAAAB8/G_giQU1cp-I/s400/BaseOK.JPG" alt="" id="BLOGGER_PHOTO_ID_5172834019005458002" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The functionality of the &lt;span style="font-weight: bold;"&gt;doSomething()&lt;/span&gt; methods isn't important right now, but assume that  the methods are implemented. What is important is that you can create an object of type &lt;span style="font-weight: bold;"&gt;DerivedClass&lt;/span&gt; and it can use the 3 &lt;span style="font-weight: bold;"&gt;doSomething()&lt;/span&gt; methods defined in the base class. This is all straightforward inheritance stuff you say, and you'd be right, but wait, the craziness is about to begin...&lt;br /&gt;&lt;br /&gt;Now, lets say I want to redefine one of the &lt;span style="font-weight: bold;"&gt;doSomething()&lt;/span&gt; methods, but I want to do it in the derived class, i.e. I want to do the following:&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp0.blogger.com/_23u4Bwj5BH0/R8mbBSjgZmI/AAAAAAAAACE/RoZt-_mOvHA/s1600-h/BaseNotOk.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp0.blogger.com/_23u4Bwj5BH0/R8mbBSjgZmI/AAAAAAAAACE/RoZt-_mOvHA/s400/BaseNotOk.JPG" alt="" id="BLOGGER_PHOTO_ID_5172836093474661986" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Again, assume that the &lt;span style="font-weight: bold;"&gt;DerivedClass&lt;/span&gt;'s &lt;span style="font-weight: bold;"&gt;doSomething()&lt;/span&gt; method is implemented. Now, assume I make an object of type &lt;span style="font-weight: bold;"&gt;DerivedClass&lt;/span&gt;. If I call the bare &lt;span style="font-weight: bold;"&gt;doSomething()&lt;/span&gt; method everything is ok, but if I try to call &lt;span style="font-weight: bold;"&gt;doSomething(int x)&lt;/span&gt; or &lt;span style="font-weight: bold;"&gt;doSomething(double x)&lt;/span&gt; the program won't compile!!&lt;br /&gt;&lt;br /&gt;This is because if you re-implement any of the functions in a base class, all other functions with the same name are now un-callable.&lt;br /&gt;&lt;br /&gt;So what, you say. Sure re-implementing non-virtual functions in derived classes is a bad idea anyway. But this behaviour is the same even if the three &lt;span style="font-weight: bold;"&gt;doSomething()&lt;/span&gt; functions are virtual. I'll say that again: &lt;span style="font-weight: bold;"&gt;this behaviour is the same even if the three doSomething() functions are virtual.&lt;/span&gt; So the following will see the exact same behaviour.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp1.blogger.com/_23u4Bwj5BH0/R8mfJijgZnI/AAAAAAAAACM/rdUUlLlLp84/s1600-h/virtualNotOk.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp1.blogger.com/_23u4Bwj5BH0/R8mfJijgZnI/AAAAAAAAACM/rdUUlLlLp84/s400/virtualNotOk.JPG" alt="" id="BLOGGER_PHOTO_ID_5172840633255093874" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The derived object is only able to call the &lt;span style="font-weight: bold;"&gt;doSomething()&lt;/span&gt; method that it defines itself, and not the other 2 defined in the base class. This seemed really strange to me, as I would have assumed that if I the &lt;span style="font-weight: bold;"&gt;doSomething()&lt;/span&gt; functions that accept the int and double could be called. After all, they are all virtual functions, which means that they are supposed to be selectively re-definable!!&lt;br /&gt;&lt;br /&gt;The reason for this strangeness is so that you do not get caught out inheriting from distant base classes by mistake. But, I'd still prefer if it was not the case as it seems kind of counter intuitive.&lt;br /&gt;&lt;br /&gt;There is a simple way around this, and it is to add in a &lt;span style="font-weight: bold;"&gt;using BaseClass::doSomething;&lt;/span&gt; in the derived class definition. This will work for both the virtial and non-virtual cases above, and will allow the &lt;span style="font-weight: bold;"&gt;DerivedClass&lt;/span&gt; to re-implement some of the &lt;span style="font-weight: bold;"&gt;doSomething()&lt;/span&gt; functions and still allow the other ones to be used.&lt;br /&gt;&lt;br /&gt;--&lt;br /&gt;Lurning Man&lt;br /&gt;--&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8117812670692388711-9185400949636591540?l=becomeaprogrammerin21years.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://becomeaprogrammerin21years.blogspot.com/feeds/9185400949636591540/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8117812670692388711&amp;postID=9185400949636591540' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/9185400949636591540'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/9185400949636591540'/><link rel='alternate' type='text/html' href='http://becomeaprogrammerin21years.blogspot.com/2008/03/c-inheritance-oddity.html' title='C++ inheritance oddity'/><author><name>Lurning Man</name><uri>http://www.blogger.com/profile/03377787338729172137</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><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp1.blogger.com/_23u4Bwj5BH0/R8mZIijgZlI/AAAAAAAAAB8/G_giQU1cp-I/s72-c/BaseOK.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8117812670692388711.post-4979426779618287769</id><published>2008-02-17T18:15:00.001Z</published><updated>2008-02-17T19:45:40.319Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='Inheritance Casting Pointers References C++'/><title type='text'>Inheritence and Casting</title><content type='html'>In order to go through inheritence and casting, I will use the following classes:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp2.blogger.com/_23u4Bwj5BH0/R7h1jm5yzwI/AAAAAAAAAAk/-KvZKwZSMyE/s1600-h/Code_base_derived.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp2.blogger.com/_23u4Bwj5BH0/R7h1jm5yzwI/AAAAAAAAAAk/-KvZKwZSMyE/s400/Code_base_derived.JPG" alt="" id="BLOGGER_PHOTO_ID_5168009827006467842" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;It is possible to create 2 objects of these classes like so:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp2.blogger.com/_23u4Bwj5BH0/R7h2dm5yzxI/AAAAAAAAAAs/qlVTbriysXs/s1600-h/Code_createObjects.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp2.blogger.com/_23u4Bwj5BH0/R7h2dm5yzxI/AAAAAAAAAAs/qlVTbriysXs/s400/Code_createObjects.JPG" alt="" id="BLOGGER_PHOTO_ID_5168010823438880530" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;You can view these classes like in the diagram below with the base object on the left and the derived object on the right. The derived class can be viewed as a base class with the additional member variables/functions of the derived class (of course it cannot access the private member variables of the base class object within it, but they do exist in memory).&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp3.blogger.com/_23u4Bwj5BH0/R7h9_25yzyI/AAAAAAAAAA0/HLTSZEFzoS8/s1600-h/BaseAndDerivedClass.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp3.blogger.com/_23u4Bwj5BH0/R7h9_25yzyI/AAAAAAAAAA0/HLTSZEFzoS8/s400/BaseAndDerivedClass.jpg" alt="" id="BLOGGER_PHOTO_ID_5168019108430794530" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;All is well and good so far. Lovely jubbly. So now we want to copy the objects. We can either copy the &lt;span style="font-weight: bold;"&gt;baseObject&lt;/span&gt; to the &lt;span style="font-weight: bold;"&gt;derivedObject&lt;/span&gt; or vice versa. One which is fine, but the other is not.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp2.blogger.com/_23u4Bwj5BH0/R7iAcm5yz1I/AAAAAAAAABM/jfIlCtfvQ20/s1600-h/Code_copyObjects.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp2.blogger.com/_23u4Bwj5BH0/R7iAcm5yz1I/AAAAAAAAABM/jfIlCtfvQ20/s400/Code_copyObjects.JPG" alt="" id="BLOGGER_PHOTO_ID_5168021801375289170" border="0" /&gt;&lt;/a&gt;Why is this so. Well, if you have a &lt;span style="font-weight: bold;"&gt;derivedObject&lt;/span&gt; and want to convert it to a &lt;span style="font-weight: bold;"&gt;baseObject&lt;/span&gt;, you can just strip away the extra functionality from the derived class and you are left with a base object.The value of &lt;span style="font-weight: bold;"&gt;baseMember&lt;/span&gt; will be of that created in &lt;span style="font-weight: bold;"&gt;derivedObject&lt;/span&gt;, i.e. 222.&lt;br /&gt;&lt;br /&gt;We cannot go in reverse though as if we want to create a derived object from a base object, where is the extra derived functionality going to come from?? All we have is a plain old base object, not a &lt;span style="font-weight: bold;"&gt;derivedMember&lt;/span&gt; in sight....&lt;br /&gt;&lt;br /&gt;Ok, hopefully that is nice and clear. Now if we want to create pointers that point to these objects we can do the following:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp2.blogger.com/_23u4Bwj5BH0/R7iETm5yz3I/AAAAAAAAABc/t-LEET0hswA/s1600-h/Code_createPointers.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp2.blogger.com/_23u4Bwj5BH0/R7iETm5yz3I/AAAAAAAAABc/t-LEET0hswA/s400/Code_createPointers.JPG" alt="" id="BLOGGER_PHOTO_ID_5168026044802977650" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;This is obviously ok, and can be viewed as such:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp0.blogger.com/_23u4Bwj5BH0/R7h-RG5yzzI/AAAAAAAAAA8/qLY4XG4H68I/s1600-h/BDCPointersGood.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp0.blogger.com/_23u4Bwj5BH0/R7h-RG5yzzI/AAAAAAAAAA8/qLY4XG4H68I/s400/BDCPointersGood.jpg" alt="" id="BLOGGER_PHOTO_ID_5168019404783537970" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;pBaseObject&lt;/span&gt; expects to point to an object of size &lt;span style="font-weight: bold;"&gt;BaseClass&lt;/span&gt;, and  &lt;span style="font-weight: bold;"&gt;pDerivedOb&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;ject &lt;/span&gt;expects to point to an object of size &lt;span style="font-weight: bold;"&gt;DerivedClass&lt;/span&gt; and whaddaya know, both of them are. All is well.&lt;br /&gt;&lt;br /&gt;So now, lets do something crazy and switch the pointers around. Point &lt;span style="font-weight: bold;"&gt;pBaseObject&lt;/span&gt; to &lt;span style="font-weight: bold;"&gt;derivedObject&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;pDerivedObject&lt;/span&gt; to &lt;span style="font-weight: bold;"&gt;baseObject&lt;/span&gt;. Again, only one of these is ok to do, and the other will result in an error. Pointing &lt;span style="font-weight: bold;"&gt;pBaseObject&lt;/span&gt; to &lt;span style="font-weight: bold;"&gt;derivedObject&lt;/span&gt; is ok as &lt;span style="font-weight: bold;"&gt;derivedObject&lt;/span&gt; also contains a baseObject, so when the BaseClass pointer points to it, it can just point to the BaseClass object contained within it (as on the left in the picture below).&lt;br /&gt;&lt;br /&gt;However, you cannot do the oppossite. If &lt;span style="font-weight: bold;"&gt;pDerivedObject&lt;/span&gt; tries to point to a BaseClass object it expects to point to something of size &lt;span style="font-weight: bold;"&gt;DerivedClass&lt;/span&gt;. Where is all of the extra derived functionality going to come from?? You cannot call &lt;span style="font-weight: bold;"&gt;setDerivedMember()&lt;/span&gt; because it does not exist for that object. This will result in an error.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp3.blogger.com/_23u4Bwj5BH0/R7h-t25yz0I/AAAAAAAAABE/X0YesbHGc2s/s1600-h/BDCPointersCast.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp3.blogger.com/_23u4Bwj5BH0/R7h-t25yz0I/AAAAAAAAABE/X0YesbHGc2s/s400/BDCPointersCast.jpg" alt="" id="BLOGGER_PHOTO_ID_5168019898704777026" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp2.blogger.com/_23u4Bwj5BH0/R7iHkm5yz4I/AAAAAAAAABk/jbDu2k2n1LI/s1600-h/Code_createPointersBad.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp2.blogger.com/_23u4Bwj5BH0/R7iHkm5yz4I/AAAAAAAAABk/jbDu2k2n1LI/s400/Code_createPointersBad.JPG" alt="" id="BLOGGER_PHOTO_ID_5168029635395637122" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The big difference between casting between pointers and copying between objects is that when you copy a derived object to a base one, all of the derived functionality gets sliced off and all that is left is the BaseClass object. When you point a BaseClass pointer to a DerivedClass object the entire DerivedClass object remains, you can point a different pointer to a DerivedClass back to it and it will still contain all of the functionality/member values.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp0.blogger.com/_23u4Bwj5BH0/R7iKAG5yz5I/AAAAAAAAABs/O4eL2JhRJaE/s1600-h/Code_createPointersBack.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp0.blogger.com/_23u4Bwj5BH0/R7iKAG5yz5I/AAAAAAAAABs/O4eL2JhRJaE/s400/Code_createPointersBack.JPG" alt="" id="BLOGGER_PHOTO_ID_5168032306865295250" border="0" /&gt;&lt;/a&gt;So, in above, &lt;span style="font-weight: bold;"&gt;pDerivedObject2&lt;/span&gt; points to the origional object with the derived class functionality intact (i.e. a &lt;span style="font-weight: bold;"&gt;derivedMember&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;baseMember&lt;/span&gt; value of 222).&lt;br /&gt;&lt;br /&gt;The rules for casting with references is the same as for casting real objects (after all, references are just another name for the objects), Thus you can assign a &lt;span style="font-weight: bold;"&gt;BaseClass&lt;/span&gt; reference to a &lt;span style="font-weight: bold;"&gt;DerivedClass&lt;/span&gt; object, but you cannot go vice versa. Note however, that assignment in this way will not 'slice' the derived object and all of the settings/functionality of it will remain intact.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp1.blogger.com/_23u4Bwj5BH0/R7iMVW5yz6I/AAAAAAAAAB0/oXp94oym1cI/s1600-h/Code_createReferences.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp1.blogger.com/_23u4Bwj5BH0/R7iMVW5yz6I/AAAAAAAAAB0/oXp94oym1cI/s400/Code_createReferences.JPG" alt="" id="BLOGGER_PHOTO_ID_5168034870960770978" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;That's all for now, hope you like the pictures, they took ages!!!&lt;br /&gt;&lt;br /&gt;--&lt;br /&gt;Lurning Man&lt;br /&gt;--&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8117812670692388711-4979426779618287769?l=becomeaprogrammerin21years.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://becomeaprogrammerin21years.blogspot.com/feeds/4979426779618287769/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8117812670692388711&amp;postID=4979426779618287769' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/4979426779618287769'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/4979426779618287769'/><link rel='alternate' type='text/html' href='http://becomeaprogrammerin21years.blogspot.com/2008/02/inheritence-and-casting.html' title='Inheritence and Casting'/><author><name>Lurning Man</name><uri>http://www.blogger.com/profile/03377787338729172137</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><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp2.blogger.com/_23u4Bwj5BH0/R7h1jm5yzwI/AAAAAAAAAAk/-KvZKwZSMyE/s72-c/Code_base_derived.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8117812670692388711.post-1689973457120759330</id><published>2008-02-14T09:43:00.007Z</published><updated>2008-02-15T22:02:33.225Z</updated><title type='text'>More Pointers and references</title><content type='html'>I talked a little last time (in Pointers and References Basics) about, well, the basics of pointers and references. I'll go into a bit of detail now about some practical uses of them.&lt;br /&gt;&lt;br /&gt;Probably the most useful thing about references/pointers is when you pass them to functions. If you declare a function like so:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;void someFunction(SomeClass a, SomeClass b);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you call that function like so:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;SomeClass a;&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;SomeClass b;&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;someFunction(a, b);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;both &lt;span style="font-weight: bold;"&gt;a&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;b&lt;/span&gt; are passed &lt;span style="font-weight: bold;"&gt;by value&lt;/span&gt; to the function. This means that the copy constructor is called, and copies of &lt;span style="font-weight: bold;"&gt;a&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;b&lt;/span&gt; are made which are used inside the function. This is desired behaviour and all, but there is some overhead involved in calling the copy constructors every time, especially in a performance intensive program (also, remember that the destructors will have to be called each time also...).&lt;br /&gt;&lt;br /&gt;So, as a solution to this you can do one of two things. You can pass a pointer to the objects into the function, or you can pass a reference to the objects to the function. This will mean changing the way the function is defined. In the case of passing a pointer it means defining:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;void someFnByPointer(SomeClass *pA, SomeClass *pB);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And for references it means defining it as:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;void someFnByRef(SomeClass &amp;amp;a, SomeClass &amp;amp;b);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;However, when calling the functions, there &lt;span style="font-weight: bold;"&gt;is&lt;/span&gt; a difference. To pass by pointer, you either have to create a pointer to the 2 objects being passed in, and pass that in, or pass in the address of the objects. To pass by reference, you call it as normal.&lt;br /&gt;&lt;br /&gt;//By pointer&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;someFnByPointer(&amp;amp;a, &amp;amp;b);&lt;/span&gt;&lt;br /&gt;//Or by reference&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;someFnByRef(a, b);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In my opinion the second way looks more natural, and you don't have to worry about making sure the pointer address is correct etc. Also in the function itself you can still use the '.' notation rather than the '-&gt;' notation. So I tend to prefer passing by reference.&lt;br /&gt;&lt;br /&gt;Actually, there is another benefit to passing by reference/pointer (from now on, I'm just going to refer to 'passing by reference', even though it is equally applicable to pointers). When passing by reference, the passed object will not get 'sliced' if the reference is to a derived object. For example, if you specify that a base class object must be passed in by value, if you pass in a derived object, the additional functionality in the derived object gets 'sliced' off (as the copy constructor will only take the values from the base class). However, if you pass by reference the copy constructor is never called and you still have the full derived object. This is especially important if there are virtual functions floating about and you want the derived ones to be called.&lt;br /&gt;So if you have a class, &lt;span style="font-weight: bold;"&gt;SomeDerivedClass&lt;/span&gt; that inherits from &lt;span style="font-weight: bold;"&gt;SomeClass&lt;/span&gt;, you can do the following and still have all of the derived class functionality (provided a derived class object is passed in):&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;void someFunction(SomeClass &amp;amp;a)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;{&lt;/span&gt;&lt;br /&gt;//if &lt;span style="font-weight: bold;"&gt;a&lt;/span&gt; is SomeDerivedClass object, then whole object&lt;br /&gt;//remains, and the derived functionality is maintained&lt;br /&gt;//so the following is fine:&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;SomeDerivedClass c = (SomeDerivedClass)a;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Finally, one last plug that should be filled regards what you can and can't do with the objects that are passed to a function. The benefit of passing by value is that when you pass an object you can forget about it, safe in the knowledge that it cannot be changed by the function. The same cannot be said if you pass by reference.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;SomeClass someObjectA, someObjectB;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;someFnByRef(someObjectA, someObjectB);&lt;/span&gt;&lt;br /&gt;//The function can do anything it wants to someObjectA&lt;br /&gt;//and someObjectB.&lt;br /&gt;&lt;br /&gt;This is clearly undesirable in many cases. &lt;span style="font-weight: bold;"&gt;someFnByRef()&lt;/span&gt; needs to guarantee that the objects passed to it won't change. To do that, just add our old friend &lt;span style="font-weight: bold;"&gt;const&lt;/span&gt; when declaring the function.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;void someFnByRef(const SomeClass &amp;amp;a, const SomeClass &amp;amp;b);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So basically you get all of the benefits of passing by reference (i.e. faster, no slicing etc.) with none of the drawbacks (it is still safe, cannot change the passed object). It's just like a late night infomercial!!&lt;br /&gt;&lt;br /&gt;(Note: For an excellent discussion on this, and many other useful topics, check out Scott Meyers' 'Effective C++')&lt;br /&gt;&lt;br /&gt;--&lt;br /&gt;Lurning Man&lt;br /&gt;--&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8117812670692388711-1689973457120759330?l=becomeaprogrammerin21years.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://becomeaprogrammerin21years.blogspot.com/feeds/1689973457120759330/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8117812670692388711&amp;postID=1689973457120759330' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/1689973457120759330'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/1689973457120759330'/><link rel='alternate' type='text/html' href='http://becomeaprogrammerin21years.blogspot.com/2008/02/more-pointers-and-references.html' title='More Pointers and references'/><author><name>Lurning Man</name><uri>http://www.blogger.com/profile/03377787338729172137</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-8117812670692388711.post-1836397583604595473</id><published>2008-02-07T18:11:00.007Z</published><updated>2008-02-14T09:43:54.684Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='Pointers References c++'/><title type='text'>C++ Pointers and References Basics</title><content type='html'>This will talk about how references and pointers relate to each other and highlight some of their differences.&lt;br /&gt;&lt;br /&gt;Pointers and references are quite similar in what they do, as in they both utilise an already existant object/variable. However, they have a number of big differences. Pointers are more flexible, but can be a lot more dangerous, whereas references are somewhat more restrictive, but still very useful.&lt;br /&gt;&lt;br /&gt;Ok, so here is an example of using both of them, starting with pointers. I will use the following class for illustration:&lt;br /&gt;class SomeClass&lt;br /&gt;{&lt;br /&gt;public:&lt;br /&gt;void setValue(int i){value = i;};&lt;br /&gt;private:&lt;br /&gt;int value;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;So you can declare an object of this class, and then a pointer to the object like this:&lt;br /&gt;SomeClass someObject;&lt;br /&gt;someObject.setValue(55);&lt;br /&gt;&lt;br /&gt;SomeClass *pToObj;    //Not pointing to anything yet&lt;br /&gt;pToObj = &amp;someObject; //Pointer is initialised now&lt;br /&gt;pToObj-&gt;setValue(11); //someObject.value is now changed&lt;br /&gt;&lt;br /&gt;Pointers can be moved around and set any amount of times, so in the above example, you could set &lt;span style="font-weight: bold;"&gt;pToObj&lt;/span&gt; to point to another object, or something different entirely (which is why they can be so dangerous).&lt;br /&gt;&lt;br /&gt;You are given a lot of freedom with pointers that can lead to problems, like the following where an unnitialized pointer is operated on:&lt;br /&gt;&lt;br /&gt;SomeClass *pToObj2;&lt;br /&gt;pToObj2-&gt;setValue(12);   //BAD IDEA - CRASH!!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;References on the other hand are much safer. They must be initialised, can only be initialised once (as a reference to some object/variable), and once they are initialised they cannot be changed.&lt;br /&gt;So you can do something like this:&lt;br /&gt;SomeClass someObject;&lt;br /&gt;SomeClass &amp;amp;someRef = someObject;&lt;br /&gt;&lt;br /&gt;But once this is done you cannot change someRef to reference another object of SomeClass (or anything else for that matter). Note that you &lt;span style="font-weight: bold;"&gt;have&lt;/span&gt; to initialise a reference. If you do not then the compiler will throw out an error.&lt;br /&gt;&lt;br /&gt;There is a difference in notation when dealing with pointers and references. When accessing member variables / functions using a pointer it is done using the arrow notation. i.e.&lt;br /&gt;&lt;br /&gt;pToObj-&gt;memberVariable;&lt;br /&gt;pToObj-&gt;memberFn();&lt;br /&gt;&lt;br /&gt;Whereas when using references, it is done using the same '.' notation as if you were using the object itself. i.e.&lt;br /&gt;&lt;br /&gt;someRef.memberVariable;&lt;br /&gt;someRef.memberFn();&lt;br /&gt;&lt;br /&gt;References can be viewed as an alias for an object. It's like making an object of the LeadSinger class called paulHewson, having him make a band with some of his friends, and then make a reference to the paulHewson object called bono.&lt;br /&gt;&lt;br /&gt;LeadSinger paulHewson;&lt;br /&gt;paulHewson.makeBand();&lt;br /&gt;LeadSinger &amp;amp;bono = paulHewson;&lt;br /&gt;bono.sing(); //Both bono and paulHewson sing here as they are the same object!!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;paulHewson&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;bono &lt;/span&gt;are just two different names for the same&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;object. You cannot then go on and make bono into something else:&lt;br /&gt;&lt;br /&gt;LeadSinger noelGallagher;&lt;br /&gt;bono = noelGallagher;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can, however, change pointers to point to other objects. So the following is possible:&lt;br /&gt;&lt;br /&gt;Drummer johnPepys;&lt;br /&gt;Drummer ericChilds;&lt;br /&gt;Drummer peterBond;&lt;br /&gt;Drummer *pSpinalTapDrummer = &amp;johnPepys;&lt;br /&gt;&lt;br /&gt;johnPepys.DieInGardeningAccident();&lt;br /&gt;pSpinalTapDrummer = &amp;ericChilds; //Same pointer, different object&lt;br /&gt;ericChilds.ChokeOnVomit();&lt;br /&gt;pSpinalTapDrummer = &amp;peterBond;&lt;br /&gt;...&lt;br /&gt;and so on&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I think that covers the basics, I've got a fair bit more to write about pointers and references in posts to come, but that's enough for now!&lt;br /&gt;&lt;br /&gt;--&lt;br /&gt;Lurning Man&lt;br /&gt;--&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8117812670692388711-1836397583604595473?l=becomeaprogrammerin21years.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://becomeaprogrammerin21years.blogspot.com/feeds/1836397583604595473/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8117812670692388711&amp;postID=1836397583604595473' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/1836397583604595473'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/1836397583604595473'/><link rel='alternate' type='text/html' href='http://becomeaprogrammerin21years.blogspot.com/2008/02/pointers-and-references-basics.html' title='C++ Pointers and References Basics'/><author><name>Lurning Man</name><uri>http://www.blogger.com/profile/03377787338729172137</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-8117812670692388711.post-262329809553477792</id><published>2008-02-05T19:34:00.000Z</published><updated>2008-02-07T13:44:52.460Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='Const C++ Learning'/><title type='text'>Const in C++</title><content type='html'>Const is great. Seriously it is.&lt;br /&gt;&lt;br /&gt;I would never have used const in my pre-professional days (in fact I had never used it before a month ago). What would have been the point? It doesn't really add any functionality or speed etc. But what it does do is try to help you to ensure you don't shoot yourself in the foot, and &lt;span style="font-family: georgia;"&gt;ensure that other people don't shoot both of you in the foot in some kind of non-const foot shooting massacre.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So, in general &lt;span style="font-weight: bold;"&gt;const&lt;/span&gt; makes a variable or object unchangable (i.e. constant). So something like this will not work:&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;span style="font-family: arial;"&gt;const int x = 5;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;x++; //Error...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;You can, of course do something like this:&lt;br /&gt;&lt;span style="font-family:arial;"&gt;const int x = 5;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int y = x;  //copies value of const variable to a non-const.&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;y++;        //Fine&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;x++;        //Still an error&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Const actually has a load of other applications, so I'll try go through them in some sort of logical order (and by that, I mean the order that Scott Meyers does in the highly recommended Effective C++....)&lt;br /&gt;&lt;br /&gt;So firstly, in some cases it can be used as a replacement for #defines. This is more of a limitation in using #defines that a huge positive in using consts. The idea that instead of doing something like:&lt;br /&gt;&lt;span style="font-family:arial;"&gt;#define PI 3.14&lt;/span&gt;&lt;br /&gt;It is possible to do this:&lt;br /&gt;&lt;span style="font-family:arial;"&gt;const double pi  3.14;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Why would you want to do this?? Well, the main reason is to stop yourself (and others) getting confused and using &lt;span style="font-weight: bold;"&gt;PI&lt;/span&gt; incorrectly, or more importantly catching times when you use it incorrectly. When you use &lt;span style="font-weight: bold;"&gt;PI&lt;/span&gt;, the value 3.14 is actually replaced in the code before the code is compiled. This means that any error messages will refer to "3.14" rather than &lt;span style="font-weight: bold;"&gt;PI&lt;/span&gt; (as the compiler, which generates the error messages, has no idea what &lt;span style="font-weight: bold;"&gt;PI&lt;/span&gt; is). Any error messages about the const &lt;span style="font-weight: bold;"&gt;pi&lt;/span&gt; will refer to that rather than some mysterious 3.14.&lt;br /&gt;&lt;br /&gt;Ok, this is a fairly small reason to use const, and to be honest I never remember getting into too much trouble using a #define for a single value, but I'm prepared to bow down to Scott Mayers advice on this one and so I wanted to include it for completeness. The real meat as far as I'm concerned is still to come.&lt;br /&gt;&lt;br /&gt;I'll now talk a little about const when declaring functions. Function declarations can put const in a number of places, as can be seen from the following function that accepts and returns an integer:&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int functionName(int x)&lt;/span&gt;&lt;span style="font-weight: bold;font-family:arial;" &gt;const&lt;/span&gt;&lt;span style="font-family:arial;"&gt;;    //&lt;span style="font-weight: bold;"&gt;(1)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int functionName(&lt;/span&gt;&lt;span style="font-weight: bold;font-family:arial;" &gt;const&lt;/span&gt;&lt;span style="font-family:arial;"&gt; int x);    //&lt;span style="font-weight: bold;"&gt;(2)&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-family:arial;" &gt;&lt;br /&gt;const&lt;/span&gt;&lt;span style="font-family:arial;"&gt; int functionName(int x);    //&lt;span style="font-weight: bold;"&gt;(3)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And here is what each of them do:&lt;br /&gt;Number (1) - When declaring a member function, if it is declared in this form then that function cannot change any of the member variables belonging to that class. It is basically like all of the member variables become const for the duration of the function. So the following will not compile:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;class SomeClass{&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;public:&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;void changeValue(int x)const;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;private:&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int someValue;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;};&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;void SomeClass::changeValue(int x)const&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;{&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;this-&gt;someValue = x; //Error - someValue is const for this function&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is very useful for member functions like 'getters' which should only read member variables and not write to them. But of course it can be used for any member function that you want to put this restriction on. Of course, careful programming would remove the necessity of this, but for the price of typing 5 letters you get the added security of &lt;span style="font-weight: bold;"&gt;knowing&lt;/span&gt; you won't change any member variables by mistake.&lt;br /&gt;&lt;br /&gt;Also, note that using const in this way is only useful on non-static member functions, so using it on a static member function (or a function outside of a class) will result in a compiler error.&lt;br /&gt;&lt;br /&gt;Ok, on to Number(2): This says that the function cannot change the value &lt;span style="font-weight: bold;"&gt;x&lt;/span&gt; that is passed to the function cannot be changed. This is useful if you are passing in a value and you want to be sure that it will not be changed by mistake. See the following code:&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;void someFunction(const int x)&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;{&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int y = x;   //Fine - const-&gt;non-const copying is fine&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;x = x + 1; //&lt;/span&gt;&lt;span style="font-weight: bold;font-family:arial;" &gt;Error&lt;/span&gt;&lt;span style="font-family:arial;"&gt; - cannot assign to a variable that is const&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Finally, there is Number(3): The first time I saw this I thought - "This must mean that when you return the const int from &lt;span style="font-weight: bold;"&gt;functionName()&lt;/span&gt;, that the return value cannot be changed". But this is wrong, wrong, wrong.... and when I thought about it it makes sense. What is actually happening when you return a variable from a method by value (as we are doing here)? Well, we are essentially copying the value from within the function to the value outside it. And copying a const value to a not const value is fine.&lt;br /&gt;&lt;br /&gt;So, in the form that it is on, statement (3) doesn't really do anything. i.e. this is perfectly legal;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;const int someFunction()&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;{&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;const int x = 5;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;return(x);&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;}&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int main(int argc, char* argv[])&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;{&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int y = someFunction();&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;y++;        //fine.&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;This is basically the same as:&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;const int constInt = 5;&lt;br /&gt;int normalInt = constInt;    //Fine, copying the value&lt;br /&gt;normalInt++;                    //Fine&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Where declaring the return type &lt;span style="font-weight: bold;"&gt;const&lt;/span&gt; is only really useful if the return value is going to be directly operated on, i.e. when returning references or pointers. So, for example, if you had a class set up as so:&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;class SomeClass{&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;public:&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;const int&amp;amp; getTimesCalled(){return(SomeClass::numTimesCalled);};&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int&amp;amp; getTimesCalledNonConst(){return(SomeClass::numTimesCalled);};&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;private:&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;static int numTimesCalled;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;};&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int SomeClass::numTimesCalled = 0;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;With a const and non-const returned reference to numTimesCalled, the following behaviour is observed:&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int main(int argc, char* argv[])&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;{&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;SomeClass someClass;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int retVal = someClass.getTimesCalled(); //Legal - the value of &lt;/span&gt;&lt;span style="font-family:arial;"&gt; &lt;/span&gt;&lt;span style="font-family:arial;"&gt;numTimesCalled&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;                                                                        //gets copied to retVal&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;retVal++;    //Legal, retVal gets incremented by 1, numTimesCalled uneffected.&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;&lt;br /&gt;retVal = someClass.getTimesCalledNonConst(); //Same as above&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;retVal++;     //Same as above&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;int &amp;amp;refRetVal = someClass.getTimesCalled(); //Illegal, cannot assign to a &lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;                                                                                 //non const reference...&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;&lt;br /&gt;int &amp;amp;refRetVal2 = someClass.getTimesCalledNonConst(); //Legal&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;refRetVal2 ++; //Legal, but changes the value numTimesCalled (in this &lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;                          //case undesired)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;const int &amp;amp;refRetValConst = someClass.getTimesCalled();    //Legal&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;refRetValConst ++; //Illegal, changing assign to a variable that is const.&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So, in this case the const return value is preffered as it stops outsiders changing the numTimesCalled variable.&lt;br /&gt;&lt;br /&gt;It also has the added advantage of stopping nasty behaviour like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;someClass.getTimesCalledNonConst()++; //Whoa, changes&lt;br /&gt;//numTimesCalled, &lt;/span&gt;&lt;span style="font-family:arial;"&gt;definitely undesired!!!&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;br /&gt;someClass.getTimesCalled()++;//Illegal cannot assign to a const (desired)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;See I told you const was great!! That's all for now.&lt;br /&gt;&lt;br /&gt;--&lt;br /&gt;Lurning Man&lt;br /&gt;--&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8117812670692388711-262329809553477792?l=becomeaprogrammerin21years.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://becomeaprogrammerin21years.blogspot.com/feeds/262329809553477792/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8117812670692388711&amp;postID=262329809553477792' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/262329809553477792'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/262329809553477792'/><link rel='alternate' type='text/html' href='http://becomeaprogrammerin21years.blogspot.com/2008/02/const-in-c.html' title='Const in C++'/><author><name>Lurning Man</name><uri>http://www.blogger.com/profile/03377787338729172137</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-8117812670692388711.post-7380229199291855151</id><published>2008-02-04T21:00:00.000Z</published><updated>2008-02-04T21:39:41.875Z</updated><title type='text'>First post</title><content type='html'>Wow, first (and possibly last, I'm pretty lazy...) post. This is exciting.....&lt;br /&gt;&lt;br /&gt;Ok, this is the most selfish reason ever to create a blog. I'm not one of these guys that wants to give something back to society through the medium of blogging or anything. Nuts to that. This is all about me!&lt;br /&gt;&lt;br /&gt;So, first the back story, and then I'll let you in on the secret of the blog (or you could just read the "About me" part on the right, but that would spoil everything...)&lt;br /&gt;&lt;br /&gt;I've been programming for a while, I did electronic engineering in college, so did bits of C and Java there (and some other stuff of course, but this is about programming). Anyway I didn't really feel like I'd had my fill of college after the degree and so I went on to do a PhD. Doing a PhD was great, I'd recommend it to anyone with an interest in research.&lt;br /&gt;&lt;br /&gt;However, in terms of learning to program, academia is not the place to be, at least in the area that I did my PhD (digital video analysis). I mean, I did a lot of programming (mostly in a hybrid of C and C++), but you only ever do enough to get the job done. In research, all you ever want to do is prove an idea works. In general it doesn't matter if it works efficiently or not, as long as it works. So you get a good idea of how to solve problems etc. but it is usually in a "I'll fiddle around with this until it works" kind of way. If it crashes every second time who cares? If it is the single most inefficient thing ever created, well, what's the problem with that?&lt;br /&gt;&lt;br /&gt;Even in the processor intensive area of digital video analysis, where you are often processing gigabytes of data, it largely doesn't matter how long the processing takes. Program running slow?? It doesn't matter, just leave it running overnight. It's 3 in the afternoon for God's sakes, there are bars to go to. Just leave the program going and you can get back to it tomorrow.&lt;br /&gt;&lt;br /&gt;And therein lies the problem. For, I have now ventured into the real world of software creation. And what a big bad world it is!!&lt;br /&gt;&lt;br /&gt;I must admit, when I decided to leave academia I kinda thought "I'll show these corporate whores a thing or two". Well, I may have been wrong on that one.... In the first couple of months of 'real work', the main thing I've learned is that I actually know very little about programming.&lt;br /&gt;&lt;br /&gt;So, as for the secret of this blog. Well, as I said, this is a selfish blog. I aim to chronicle my journey from a basic programmer to (hopefully) an accomplished one. Whenever I find out something that I didn't know before, I'll post something up here for everyone to marvel at how silly I am for not knowing it in the first place. Thus, the basic aim of this blog is to re-enforce my own learning.&lt;br /&gt;&lt;br /&gt;It won't be pretty, but it might be useful (for me)....&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8117812670692388711-7380229199291855151?l=becomeaprogrammerin21years.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://becomeaprogrammerin21years.blogspot.com/feeds/7380229199291855151/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8117812670692388711&amp;postID=7380229199291855151' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/7380229199291855151'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8117812670692388711/posts/default/7380229199291855151'/><link rel='alternate' type='text/html' href='http://becomeaprogrammerin21years.blogspot.com/2008/02/first-post.html' title='First post'/><author><name>Lurning Man</name><uri>http://www.blogger.com/profile/03377787338729172137</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>
