<?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:blogger='http://schemas.google.com/blogger/2008' 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-5240770843138558900</id><updated>2024-09-05T02:24:32.789-07:00</updated><category term="C++"/><category term="Published"/><category term="Computer"/><category term="Inheritance"/><category term="Programming"/><category term="STL"/><category term="Virtual"/><category term="template"/><category term="Design Patterns"/><category term="Multiple"/><category term="OOPS"/><category term="Object"/><category term="default argument"/><category term="development"/><category term="for_each"/><category term="function object"/><category term="functor"/><category term="list"/><category term="operator overloading"/><category term="remove()"/><category term="software"/><title type='text'>OOP&#39;S Design</title><subtitle type='html'>Developer perspective to OO Software development.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://oops-d.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://oops-d.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Manav Prakash Sah</name><uri>http://www.blogger.com/profile/04457020170692209577</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5240770843138558900.post-7999304725121896959</id><published>2012-04-30T11:20:00.000-07:00</published><updated>2012-04-30T11:20:10.856-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="for_each"/><category scheme="http://www.blogger.com/atom/ns#" term="function object"/><category scheme="http://www.blogger.com/atom/ns#" term="functor"/><category scheme="http://www.blogger.com/atom/ns#" term="STL"/><category scheme="http://www.blogger.com/atom/ns#" term="template"/><title type='text'>C++ functors: which one will be invoked ?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;i style=&quot;text-align: -webkit-auto;&quot;&gt;Function Object:&lt;/i&gt;&lt;/h2&gt;
In simple term we can define as :&lt;br /&gt;
&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;A&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;text-align: -webkit-auto;&quot;&gt;Function Object&lt;/i&gt;&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;, or&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;text-align: -webkit-auto;&quot;&gt;Functor&lt;/i&gt;&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;&amp;nbsp;(the two terms are synonymous) is simply any object that can be called as if it is a function. An ordinary function is a function object, and so is a function pointer; more generally, so is an object of a class that defines&amp;nbsp;&lt;/span&gt;&lt;tt style=&quot;text-align: -webkit-auto;&quot;&gt;operator()&lt;/tt&gt;&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
A very simple example can be :-&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;class DeleteObject{&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; public:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; template&amp;lt;typename T&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; void operator() ( const T&amp;amp; ptr) const { delete ptr; }&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;};&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;vector&amp;lt;Drive *&amp;gt; vec;&lt;/b&gt; // our supposed definition of vector&lt;br /&gt;
&lt;br /&gt;
Above functor can be used with for_each as :&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;b&gt; for_each(vec.begin(), vec.end(), DeleteObject());&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
here on each element of vec, delete operator will be invoked.&lt;br /&gt;
and every thing is fine, assuming after for_each vec.clear() is called.&lt;br /&gt;
&lt;br /&gt;
if we change the definition of DeleteObject as follow:&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;class DeleteObject{&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; public:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;template&amp;lt;typename T&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; void operator() ( const T* ptr) const {&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &quot;del &quot;&amp;lt;&amp;lt; endl; &amp;nbsp; &amp;nbsp; &amp;nbsp; delete ptr;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; }&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;};&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
and perform the same operation as above, things work fine again.&lt;br /&gt;
&lt;br /&gt;
Now we change our definition as :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;class DeleteObject{&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; public:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; template&amp;lt;typename T&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; void operator() ( const T&amp;amp; ptr) const { delete ptr; }&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; template&amp;lt;typename T&amp;gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; void operator() ( const T* ptr) const {&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; &quot;del &quot;&amp;lt;&amp;lt; endl; &amp;nbsp; &amp;nbsp; &amp;nbsp; delete ptr;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; }&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;};&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
and perform the same operation as above, things work fine again and&amp;nbsp;&lt;b&gt;void operator() ( const T&amp;amp; ptr) get invoked &lt;/b&gt;and it worked fine.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now my question that is puzzling me how compiler decide which one to invoked from:&lt;br /&gt;
&amp;nbsp;&lt;b&gt;&amp;nbsp;void operator() ( const T&amp;amp; ptr) const;&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&lt;b&gt;void operator() ( const T* ptr) const ;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
and what difference these make?&lt;br /&gt;
&lt;br /&gt;
any suggestion and response will be highly appriciated.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://oops-d.blogspot.com/feeds/7999304725121896959/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://oops-d.blogspot.com/2012/04/c-functors-which-one-will-be-invoked.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/7999304725121896959'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/7999304725121896959'/><link rel='alternate' type='text/html' href='http://oops-d.blogspot.com/2012/04/c-functors-which-one-will-be-invoked.html' title='C++ functors: which one will be invoked ?'/><author><name>Manav Prakash Sah</name><uri>http://www.blogger.com/profile/04457020170692209577</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5240770843138558900.post-2370636467646133831</id><published>2012-02-03T08:01:00.000-08:00</published><updated>2012-02-03T08:01:33.230-08:00</updated><title type='text'>OO Software Design: Inheritance and Generalization vs Specialization</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: &#39;Trebuchet MS&#39;, sans-serif; font-size: large;&quot;&gt;&lt;b&gt;&lt;i&gt;Inheritance:&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
When ever we any of us think about OOPS, Inheritance is one of the first concept that click in.&lt;br /&gt;
&quot;&lt;span style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;The ability for a&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px; text-align: -webkit-auto;&quot;&gt;class&lt;/i&gt;&lt;span style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;&amp;nbsp;to extend or override functionality of another&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px; text-align: -webkit-auto;&quot;&gt;class&lt;/i&gt;&lt;span style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;&quot;&gt;.&quot;&lt;/span&gt;&lt;br /&gt;
Inheritance is a way to reuse code of existing objects. Pre-existing class is called base class or super class and the new class is knows as derived class or subclass. The relationships of classes through inheritance gives birth to hierarchy.&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://www.uml-diagrams.org/class-diagrams/class-generalizaion-shared.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://www.uml-diagrams.org/class-diagrams/class-generalizaion-shared.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Inheritance&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Here Account is Base class and Checking A/c, Saving A/c, Credit A/c are derived classes from base.&lt;br /&gt;
In general terms, generalization and specialization both refers to inheritance but the approach in which they are implemented are different. If we have many object with similar&amp;nbsp;behavior or properties and we craft a supper class by combining the common properties or behavior to cater the need of sub classes then it is called the Generalization.&lt;br /&gt;
In contrast to Generalization if new classes are created from existing class to perform or implement &amp;nbsp;a specific feature this process is called Specialization. Both generalization and specialization are implemented using inheritance but the order of creation of the subclass and&amp;nbsp;super-class&amp;nbsp;drives the concept name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Trebuchet MS&#39;, sans-serif; font-size: large;&quot;&gt;&lt;b&gt;Generalization:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Generalization is the process of extracting common characteristic from two or more classes and combining them into a generalized super class. Common characteristic can be attributes or&amp;nbsp;behavior.&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://sites.google.com/site/ignousolvedassignments/_/rsrc/1301327698486/images/inheritance.GIF&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;141&quot; src=&quot;https://sites.google.com/site/ignousolvedassignments/_/rsrc/1301327698486/images/inheritance.GIF&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Consider the above figure, The Car and Scooter partially share common properties. During generalization all the common characteristic are combined and used to create a new class here say Vehicle. Car and Scooter become subclass of Vehicle.&lt;br /&gt;
In other word Generalization is all about bottom-up approach and identifying&amp;nbsp;generic/common class. Specialization is just a reverse process ie. Top-Down approach. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Trebuchet MS&#39;, sans-serif; font-size: large;&quot;&gt;&lt;b&gt;Specialization:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Specialization is just the other side of generalization coin: Vehicle Generalize what is common between Car and Scooter, Car and Scooter specialize Vehicle to there own&amp;nbsp;sub-type.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://oops-d.blogspot.com/feeds/2370636467646133831/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://oops-d.blogspot.com/2012/02/oo-software-design-inheritance-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/2370636467646133831'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/2370636467646133831'/><link rel='alternate' type='text/html' href='http://oops-d.blogspot.com/2012/02/oo-software-design-inheritance-and.html' title='OO Software Design: Inheritance and Generalization vs Specialization'/><author><name>Manav Prakash Sah</name><uri>http://www.blogger.com/profile/04457020170692209577</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5240770843138558900.post-1748016045309853920</id><published>2012-01-24T10:55:00.000-08:00</published><updated>2012-02-02T19:18:46.259-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Computer"/><category scheme="http://www.blogger.com/atom/ns#" term="default argument"/><category scheme="http://www.blogger.com/atom/ns#" term="Inheritance"/><category scheme="http://www.blogger.com/atom/ns#" term="Published"/><category scheme="http://www.blogger.com/atom/ns#" term="Virtual"/><title type='text'>Argument Resolution virtual Function</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Consider the following program:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;stdio.h&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;enum&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;ShapeColor&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Red&lt;/span&gt;,&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Green&lt;/span&gt;,&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Blue&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;};&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Shape&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;virtual&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;draw&lt;/span&gt;(&lt;span style=&quot;color: purple;&quot;&gt;ShapeColor&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;color=&lt;span style=&quot;color: purple;&quot;&gt;Red&lt;/span&gt;)&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;=&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;0&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;PrintColor(&lt;span style=&quot;color: purple;&quot;&gt;ShapeColor&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;c)&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;switch&lt;/span&gt;(c)&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;case&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Red&lt;/span&gt;:&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;printf(&lt;span style=&quot;color: green;&quot;&gt;&quot;Red\n&quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;break&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;case&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Green&lt;/span&gt;:&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;printf(&lt;span style=&quot;color: green;&quot;&gt;&quot;Green\n&quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;break&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;default&lt;/span&gt;:&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;printf(&lt;span style=&quot;color: green;&quot;&gt;&quot;Blue\n&quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;       &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;};&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Rectangle&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;:&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;draw&lt;/span&gt;(&lt;span style=&quot;color: purple;&quot;&gt;ShapeColor&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;color=&lt;span style=&quot;color: purple;&quot;&gt;Green&lt;/span&gt;)&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;printf(&lt;span style=&quot;color: green;&quot;&gt;&quot;From&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;Rectangle\n&quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;PrintColor(color);&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;};&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Circle&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;:&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;draw&lt;/span&gt;(&lt;span style=&quot;color: purple;&quot;&gt;ShapeColor&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;color=&lt;span style=&quot;color: purple;&quot;&gt;Green&lt;/span&gt;)&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;printf(&lt;span style=&quot;color: green;&quot;&gt;&quot;From&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;Circle\n&quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;PrintColor(color);&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;};&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;main()&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Rectangle&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;r;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;*ps&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;=&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;amp;r;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;ps-&amp;gt;&lt;span style=&quot;font-style: italic;&quot;&gt;draw&lt;/span&gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Circle&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;c;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;ps&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;=&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;amp;c;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;ps-&amp;gt;&lt;span style=&quot;font-style: italic;&quot;&gt;draw&lt;/span&gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;0&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Output of above code is as below:&lt;/pre&gt;
&lt;pre&gt;From Rectangle
Red
From Circle
Red&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;From the output of above program it is clear that for virtual function, actual&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;function called is resolved at runtime but the default value of the argument is&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;get resolved in compilation time.&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Now this time I leave this on you to analysis and comment on this program its output and default value resolution of virtual function at compile time as well as on run-time.&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Waiting for you comment.&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://oops-d.blogspot.com/feeds/1748016045309853920/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://oops-d.blogspot.com/2012/01/argument-resolution-virtual-function.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/1748016045309853920'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/1748016045309853920'/><link rel='alternate' type='text/html' href='http://oops-d.blogspot.com/2012/01/argument-resolution-virtual-function.html' title='Argument Resolution virtual Function'/><author><name>Manav Prakash Sah</name><uri>http://www.blogger.com/profile/04457020170692209577</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Mayur Vihar II, New Delhi, Delhi, India</georss:featurename><georss:point>28.620240545725611 77.299289703369141</georss:point><georss:box>28.613271045725611 77.289419203369135 28.627210045725612 77.309160203369146</georss:box></entry><entry><id>tag:blogger.com,1999:blog-5240770843138558900.post-3130343817666152058</id><published>2012-01-21T21:04:00.000-08:00</published><updated>2012-02-02T19:18:46.265-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Computer"/><category scheme="http://www.blogger.com/atom/ns#" term="Inheritance"/><category scheme="http://www.blogger.com/atom/ns#" term="Multiple"/><category scheme="http://www.blogger.com/atom/ns#" term="Object"/><category scheme="http://www.blogger.com/atom/ns#" term="OOPS"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><category scheme="http://www.blogger.com/atom/ns#" term="Published"/><category scheme="http://www.blogger.com/atom/ns#" term="Virtual"/><title type='text'>C++ Multiple Inheritance and Class size</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
While learning or&amp;nbsp;developing&amp;nbsp;any C++ application you must have
used the Multiple-inheritance feature provided by language. Lot
has been written about it&amp;nbsp;in many places how it do it, how it works and
what is what &amp;nbsp;in multiple inheritance.&lt;br /&gt;
There is a very basic thing with respect to multiple inheritance what will
be the size of object ?&lt;br /&gt;
Consider following hierarchy.&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;using&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;std&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Base&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;a&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;};&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;X&lt;/span&gt;:&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Base&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;x&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;};&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Y&lt;/span&gt;:&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Base&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;y&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;};&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Z&lt;/span&gt;:&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;X&lt;/span&gt;,&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Y&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;{&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;};&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;main(){&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Z&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;z;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;lt;&amp;lt;&lt;span style=&quot;color: green;&quot;&gt;&quot;size&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;z&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;&lt;/span&gt;&amp;lt;&amp;lt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;sizeof&lt;/span&gt;(z)&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;lt;&amp;lt;endl;&lt;/pre&gt;
&lt;pre&gt;return 0;&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;pre&gt;Definitely the output will be 16 considering 32 bit int.&lt;/pre&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoNormalTable&quot; style=&quot;border-collapse: collapse; margin-left: 5.0pt; mso-padding-alt: 0in 0in 0in 0in; width: 81.52%;&quot;&gt;
 &lt;tbody&gt;
&lt;tr style=&quot;height: 10.0pt; mso-yfti-firstrow: yes; mso-yfti-irow: 0;&quot;&gt;
  &lt;td style=&quot;border: solid black 1.0pt; height: 10.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt;&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;Class Name&lt;/b&gt;&lt;/pre&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid black 1.0pt; height: 10.0pt; mso-border-left-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 2.0pt 2.0pt;&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;Size in Bytes(32 bit integer)&lt;/b&gt;&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 9.35pt; mso-yfti-irow: 1;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 9.35pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt;&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;Base&lt;/pre&gt;
&lt;/td&gt;
  
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 9.35pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt;&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;4 ( sizeof(a) )&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 10.0pt; mso-yfti-irow: 2;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 10.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt;&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;X&lt;/pre&gt;
&lt;/td&gt;
  
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 10.0pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt;&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;8 ( sizeof(x) + sizeof(Base) )&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 10.0pt; mso-yfti-irow: 3;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 10.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt;&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;Y&lt;/pre&gt;
&lt;/td&gt;
  
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 10.0pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt;&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;8 ( sizeof(y) + sizeof(Base) )&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 8.85pt; mso-yfti-irow: 4; mso-yfti-lastrow: yes;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 8.85pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt;&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;Z&lt;/pre&gt;
&lt;/td&gt;
  
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 8.85pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt;&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;16 ( sizeof(X) + sizeof(Y) )&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;In virtual base class there will be only single object and all the classes which derives&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;virtual base class requires a pointer to it so that they can access it. &lt;/pre&gt;
Consider following hierarchy using virtual inheritance as follow:&lt;br /&gt;
&lt;span style=&quot;color: navy;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;br /&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;using&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;};&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;virtual&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;virtual&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;};&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;virtual&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;virtual&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;};&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;E&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;};&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;F&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{};&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;G&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;virtual&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;virtual&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{};&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;main&lt;span style=&quot;color: black;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;E&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;F&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;G&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;g&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;A&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;B&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;C&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;D&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;E&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;E&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;F&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;F&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;G&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;G&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;&quot;&gt;&lt;/pre&gt;
Output:&lt;br /&gt;
&lt;pre&gt; size of A4&lt;/pre&gt;
&lt;pre&gt; size of B12&lt;/pre&gt;
&lt;pre&gt; size of C12&lt;/pre&gt;
&lt;pre&gt; size of D28&lt;/pre&gt;
&lt;pre&gt; size of E24&lt;/pre&gt;
&lt;pre&gt; size of F20&lt;/pre&gt;
&lt;pre&gt; size of G24&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;Memory Layout:&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;(gdb) display a&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;1: a = {a = -6800}&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;(gdb) display b&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;2: b = {&amp;lt;A&amp;gt; = {a = 0}, _vptr.B = 0x401188, b = 4196304}&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;(gdb) display c&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;3: c = {&amp;lt;A&amp;gt; = {a = 0}, _vptr.C = 0x401168, c = 4197632}&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;(gdb) display d&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;4: d = {&amp;lt;B&amp;gt; = {&amp;lt;A&amp;gt; = {a = 0},&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;               _vptr.B = 0x4010c0, b = 6296960},&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;        &amp;lt;C&amp;gt; = {_vptr.C = 0x4010d8, c = 608253861},&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;        _vptr.D = 0x4010a8, d = 0}&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;(gdb) display e&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;5: e = {&amp;lt;B&amp;gt; = {&amp;lt;A&amp;gt; = {a = 6298088},&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;                      _vptr.B = 0x400ff8, b = 6},&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;        &amp;lt;C&amp;gt; = {_vptr.C = 0x401010, c = 4196224},&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;        e = 0}&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;(gdb) display f&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;6: f = {&amp;lt;B&amp;gt; = {&amp;lt;A&amp;gt; = {a = 0},&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;               _vptr.B = 0x400f58, b = 4196998},&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;        &amp;lt;C&amp;gt; = {_vptr.C = 0x400f70, c = 4197725},&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;        &amp;lt;No data fields&amp;gt;}&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;(gdb) display g&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;7: g = {&amp;lt;B&amp;gt; = {&amp;lt;A&amp;gt; = {a = 0},&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;               _vptr.B = 0x400ea0, b = 6296960},&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;        &amp;lt;C&amp;gt; = {_vptr.C = 0x400eb8, c = 1},&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b&gt;        _vptr.G = 0x400e88}&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;&amp;nbsp;&lt;/b&gt;&lt;/pre&gt;
&lt;pre&gt;Here is a memory size table for individual class:&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoNormalTable&quot; style=&quot;border-collapse: collapse; margin-left: 5.0pt; mso-padding-alt: 0in 0in 0in 0in; width: 81.52%;&quot;&gt;
 &lt;tbody&gt;
&lt;tr style=&quot;height: 10.0pt; mso-yfti-firstrow: yes; mso-yfti-irow: 0;&quot;&gt;
  &lt;td style=&quot;border: solid black 1.0pt; height: 10.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 17.48%;&quot; valign=&quot;top&quot; width=&quot;17%&quot;&gt;&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;Class Name&lt;/b&gt;&lt;/pre&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid black 1.0pt; height: 10.0pt; mso-border-left-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 2.0pt 2.0pt; width: 82.52%;&quot; valign=&quot;top&quot; width=&quot;82%&quot;&gt;&lt;pre&gt;&lt;b style=&quot;mso-bidi-font-weight: normal;&quot;&gt;Size in Bytes(32 bit integer) &lt;/b&gt;&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 9.35pt; mso-yfti-irow: 1;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 9.35pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 17.48%;&quot; valign=&quot;top&quot; width=&quot;17%&quot;&gt;&lt;pre&gt;A&lt;/pre&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 9.35pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 82.52%;&quot; valign=&quot;top&quot; width=&quot;82%&quot;&gt;&lt;pre&gt;4 ( sizeof(A) )&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 10.0pt; mso-yfti-irow: 2;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 10.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 17.48%;&quot; valign=&quot;top&quot; width=&quot;17%&quot;&gt;&lt;pre&gt;B&lt;/pre&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 10.0pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 82.52%;&quot; valign=&quot;top&quot; width=&quot;82%&quot;&gt;&lt;pre&gt;12 (sizeof(A *) + sizeof(vptr.B) + sizeof(int))&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 10.0pt; mso-yfti-irow: 3;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 10.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 17.48%;&quot; valign=&quot;top&quot; width=&quot;17%&quot;&gt;&lt;pre&gt;C&lt;/pre&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 10.0pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 82.52%;&quot; valign=&quot;top&quot; width=&quot;82%&quot;&gt;&lt;pre&gt;12 (sizeof(A *) + sizeof(vptr.C) + sizeof(int))&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 8.85pt; mso-yfti-irow: 4;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 8.85pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 17.48%;&quot; valign=&quot;top&quot; width=&quot;17%&quot;&gt;&lt;pre&gt;D&lt;/pre&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 8.85pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 82.52%;&quot; valign=&quot;top&quot; width=&quot;82%&quot;&gt;&lt;pre&gt;28 (sizeof(A *) + (sizeof(B *) + sizeof(vptr.B) &amp;nbsp;&amp;nbsp;+ sizeof(C *) + sizeof(vptr.C)+ sizeof(vptr.D) + sizeof(int))&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 8.85pt; mso-yfti-irow: 5;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 8.85pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 17.48%;&quot; valign=&quot;top&quot; width=&quot;17%&quot;&gt;&lt;pre&gt;E&lt;/pre&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 8.85pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 82.52%;&quot; valign=&quot;top&quot; width=&quot;82%&quot;&gt;&lt;pre&gt;24 (sizeof(A *) + (sizeof(B *) + sizeof(vptr.B) &amp;nbsp;&amp;nbsp;+ sizeof(C *) + sizeof(vptr.C) + sizeof(int))&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 8.85pt; mso-yfti-irow: 6;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 8.85pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 17.48%;&quot; valign=&quot;top&quot; width=&quot;17%&quot;&gt;&lt;pre&gt;F&lt;/pre&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 8.85pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 82.52%;&quot; valign=&quot;top&quot; width=&quot;82%&quot;&gt;&lt;pre&gt;20 (sizeof(A *) + (sizeof(B *) + sizeof(vptr.B) &amp;nbsp;&amp;nbsp;+ sizeof(C *) + sizeof(vptr.C))&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 8.85pt; mso-yfti-irow: 7; mso-yfti-lastrow: yes;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid black 1.0pt; height: 8.85pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 17.48%;&quot; valign=&quot;top&quot; width=&quot;17%&quot;&gt;&lt;pre&gt;G&lt;/pre&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; height: 8.85pt; mso-border-left-alt: solid black 1.0pt; mso-border-top-alt: solid black 1.0pt; padding: 5.0pt 5.0pt 5.0pt 5.0pt; width: 82.52%;&quot; valign=&quot;top&quot; width=&quot;82%&quot;&gt;&lt;pre&gt;28 (sizeof(A *) + (sizeof(B *) + sizeof(vptr.B) &amp;nbsp;&amp;nbsp;+ sizeof(C *) + sizeof(vptr.C)+ sizeof(vptr.D))&lt;/pre&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Above table explains how the size is calculated in Multiple
inheritance.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
*** In some compiler size may vary due to padding done by compiler for memory alignment.&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://oops-d.blogspot.com/feeds/3130343817666152058/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://oops-d.blogspot.com/2012/01/c-multiple-inheritance-and-class-size.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/3130343817666152058'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/3130343817666152058'/><link rel='alternate' type='text/html' href='http://oops-d.blogspot.com/2012/01/c-multiple-inheritance-and-class-size.html' title='C++ Multiple Inheritance and Class size'/><author><name>Manav Prakash Sah</name><uri>http://www.blogger.com/profile/04457020170692209577</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Nainital, Uttaranchal, India</georss:featurename><georss:point>29.393392954566497 79.449820518493652</georss:point><georss:box>29.391663454566498 79.447353018493658 29.395122454566497 79.452288018493647</georss:box></entry><entry><id>tag:blogger.com,1999:blog-5240770843138558900.post-1059488212847041758</id><published>2012-01-19T19:02:00.000-08:00</published><updated>2012-02-02T19:18:46.247-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Published"/><title type='text'>Explicit Constructor C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
The C++ is designed in such a way that by default,a single argument constructor also defines an implicit conversion. In C++, an implicit conversion is a conversion from one type to another that doesn&#39;t require an explicit typecast. For example consider following class:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Conversion&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Conversion&lt;/span&gt;(&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;o=&lt;span style=&quot;color: navy;&quot;&gt;0&lt;/span&gt;)&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;:&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;data&lt;/span&gt;(o)&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;getData()&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;data&lt;/span&gt;;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;private&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;data&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;};&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;main(){&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Conversion&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;c&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;=&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;5&lt;/span&gt;;   // initialize c with &lt;span style=&quot;color: purple;&quot;&gt;Conversion(5)&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;lt;&amp;lt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;c.getData()&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;lt;&amp;lt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;0&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Here the constructor worked as implicit converter.&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;But there are cases where this feature can play bad. Consider following:&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;str&lt;span style=&quot;color: black;&quot;&gt;);        // initial val is c-style string  str &lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;);&lt;/span&gt;        // pre-allocate len bytes
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;getData&lt;span style=&quot;color: black;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;}&lt;/span&gt;
&lt;span style=&quot;color: olive;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;unsigned&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: black;&quot;&gt;};&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;];&lt;/span&gt;
&lt;span style=&quot;color: black;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;strlen&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;];&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;strcpy&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: black;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;main&lt;span style=&quot;color: black;&quot;&gt;()&lt;/span&gt;
&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;m_str&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;it&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;works&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;here&quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt; //&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;m_str&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;.&lt;/span&gt;getData&lt;span style=&quot;color: black;&quot;&gt;()&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;m_str2&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;            //&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt;&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;m_str2&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;.&lt;/span&gt;getData&lt;span style=&quot;color: black;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: black;&quot;&gt;}&lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Now, &lt;span style=&quot;color: silver;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;m_str2&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;; makes m_str2 a string with &#39;a&#39; char, its quite unlikely&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: black;&quot;&gt;that this is what person want. So make sure that the conversion is intuitive and
consistent with major C++ conventions. If not, consider using non-constructor member functions.&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;
To prevent above discussed problem C++ comes up with &lt;i&gt;explicit&lt;/i&gt; keyword, that suppressed the implicit conversion. That is, an explicit constructor will be invoked only explicitly. So if we modify above class declaration as follow:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;str&lt;span style=&quot;color: black;&quot;&gt;);        // initial val is c-style string  str &lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;explicit &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;);&lt;/span&gt;        // pre-allocate len bytes
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;getData&lt;span style=&quot;color: black;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;}&lt;/span&gt;
&lt;span style=&quot;color: olive;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;unsigned&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: black;&quot;&gt;};&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;
Now for line &lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;MyString&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;m_str2&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;    &lt;/pre&gt;
&lt;br /&gt;
We will get compiler error since there&#39;s no implicit conversion from char to int. How ever we can still write&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: purple;&quot;&gt;    MyString&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;m_str2&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;);&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;Which is what we were trying to accomplish in the first place.&lt;/pre&gt;
&lt;pre&gt; &lt;/pre&gt;
&lt;pre&gt;When designing classes, if you have a single-argument constructor that is not intended&lt;/pre&gt;
&lt;pre&gt; as a conversion function, you must mark it explicit to avoid running into the “implicit conversion” trap. &lt;/pre&gt;
&lt;br /&gt;
&lt;pre style=&quot;text-align: left;&quot;&gt;&lt;/pre&gt;
&lt;pre style=&quot;text-align: left;&quot;&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://oops-d.blogspot.com/feeds/1059488212847041758/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://oops-d.blogspot.com/2012/01/explicit-constructor-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/1059488212847041758'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/1059488212847041758'/><link rel='alternate' type='text/html' href='http://oops-d.blogspot.com/2012/01/explicit-constructor-c.html' title='Explicit Constructor C++'/><author><name>Manav Prakash Sah</name><uri>http://www.blogger.com/profile/04457020170692209577</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5240770843138558900.post-6992595527883921393</id><published>2012-01-16T11:36:00.000-08:00</published><updated>2012-01-16T11:36:31.904-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="operator overloading"/><category scheme="http://www.blogger.com/atom/ns#" term="remove()"/><category scheme="http://www.blogger.com/atom/ns#" term="STL"/><category scheme="http://www.blogger.com/atom/ns#" term="template"/><title type='text'>STL List remove()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;STL Lists are a kind of sequence container and among the most widely used.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Here i will discuss just about one function remove.&lt;/span&gt;&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;void remove(const T&amp;amp; value)&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;SGI says: Removes all elements that compare equal to&amp;nbsp;&lt;/span&gt;&lt;tt style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;val&lt;/tt&gt;&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;. The relative order of elements that are not removed is unchanged, and iterators to elements that are not removed remain valid. This function is linear time: it performs exactly&amp;nbsp;&lt;/span&gt;&lt;tt style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;size()&lt;/tt&gt;&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;&amp;nbsp;comparisons for equality.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: white; font-family: inherit; text-align: -webkit-auto;&quot;&gt;&amp;nbsp; &amp;nbsp; So if we have a list of integer say:&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: inherit; text-align: -webkit-auto;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; lst: 1,2, 4,6 4,5,9&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; and we say &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;lst.remove(4);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;&amp;nbsp; &amp;nbsp; then remaining list be&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;lst: 1,2,6,5,9&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; font-family: inherit; text-align: -webkit-auto;&quot;&gt;Internally remove function iterate complete list, compare and remove all elements equal to 4. &amp;nbsp;With&amp;nbsp;intrinsic&amp;nbsp;/in-built data-type simple comparison provided by c++ works fine.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit; text-align: -webkit-auto;&quot;&gt;Now what if we have a list of &amp;nbsp;class object.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;As in following example:&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy; font-family: inherit;&quot;&gt;#include &quot;iostream&quot;
#include &quot;list&quot;
using namespace std;&lt;span style=&quot;font-size: xx-small;&quot;&gt;
&lt;/span&gt;class lstTest {
public:
    lstTest(int d=0):i(d) {}
    int getData() const {return i;} 
private:
    int i;
};
int main() {
    list&lt;lsttest&gt; lstObj;
    list&lt;lsttest&gt;::iterator itr;
    lstTest t(10);
    lstObj.push_back(lstTest(11));
    lstObj.push_back(lstTest(2));
    lstObj.push_back(t);
    lstObj.push_back(lstTest(11));
    for(itr=lstObj.begin(); itr!= lstObj.end();itr++)
        cout &amp;lt;&amp;lt; (*itr).getData() &amp;lt;&amp;lt; endl;
    lstObj.remove(t);
    for(itr=lstObj.begin(); itr!= lstObj.end();itr++)
        cout &amp;lt;&amp;lt; (*itr).getData() &amp;lt;&amp;lt; endl;
    return 0;
}&lt;/lsttest&gt;&lt;/lsttest&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy; font-family: inherit;&quot;&gt;on compilation we get following error:&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy; font-family: inherit;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy; font-family: inherit;&quot;&gt;/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/list.tcc: In member function â€˜void std::list&amp;lt;_Tp, _Alloc&amp;gt;::remove(const value_type&amp;amp;) [with _Tp = lstTest, _Alloc = std::allocator&lt;lsttest&gt;, std::list&amp;lt;_Tp, _Alloc&amp;gt;::value_type = lstTest]â€™:
listTest.cpp:25:20:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/list.tcc:249:4: error: no match for â€˜operator==â€™ in â€˜__first.std::_List_iterator&amp;lt;_Tp&amp;gt;::operator* [with _Tp = lstTest, std::_List_iterator&amp;lt;_Tp&amp;gt;::reference = lstTest&amp;amp;]() == __valueâ€™
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/list.tcc:249:4: note: candidates are:
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/postypes.h:218:5: note: template&lt;class _statet=&quot;&quot;&gt; bool std::operator==(const std::fpos&amp;lt;_StateT&amp;gt;&amp;amp;, const std::fpos&amp;lt;_StateT&amp;gt;&amp;amp;)&lt;/class&gt;&lt;/lsttest&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy; font-family: inherit;&quot;&gt;...&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy; font-family: inherit;&quot;&gt;...&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy; font-family: inherit;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;so why this Kolavery..... Kolavery ji...&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;As i discussed earlier remove perform &lt;span style=&quot;background-color: white; text-align: -webkit-auto;&quot;&gt;comparisons for equality so for UDT we need to help remove(&lt;span style=&quot;font-size: xx-small;&quot;&gt;...&lt;/span&gt;) by providing &lt;/span&gt;&lt;/span&gt;customize &quot;==&quot; operator for class in question.&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;For this we need to implement bool operator ==(...) function for class.&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;So we add following function to  above class:&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;&lt;span style=&quot;color: #20124d;&quot;&gt;class lstTest {
public:
    lstTest(int d=0):i(d) {}
    int getData() const {return i;}
    &lt;b&gt;&lt;i&gt;friend bool operator==(const lstTest&amp;amp; o1, const lstTest&amp;amp; o2);&lt;/i&gt;&lt;/b&gt;
private:
    int i;
};
&lt;b&gt;&lt;i&gt;bool operator==(const lstTest&amp;amp; o1, const lstTest&amp;amp; o2) {
        return o1.i== o2.i;
}&lt;/i&gt;&lt;/b&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;After this &quot;==&quot; overloading above code will works fine.&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;Now what if you want some thing like this with same class:&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;&lt;pre style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;color: navy; font-family: inherit;&quot;&gt;    lstObj.remove(11); // removing all object with value of i  = 11&lt;/span&gt;&lt;/pre&gt;
&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;For this we again need to implement another &quot;==&quot; operator for class as follow:&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;function declaration in class:&lt;/pre&gt;
&lt;blockquote&gt;
 &lt;span style=&quot;color: #20124d;&quot;&gt;friend bool operator==(const lstTest&amp;amp; o1, const int&amp;amp; i);&lt;/span&gt;&lt;/blockquote&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;function definition:&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;color: #20124d;&quot;&gt;bool operator==(const lstTest&amp;amp; o1, const int&amp;amp; i) {&lt;/span&gt;&lt;span style=&quot;color: #20124d;&quot;&gt;        return o1.i== i;&lt;/span&gt;&lt;span style=&quot;color: #20124d;&quot;&gt;}&lt;/span&gt;&lt;/blockquote&gt;
&lt;span style=&quot;color: #20124d;&quot;&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;/pre&gt;
&lt;pre style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://oops-d.blogspot.com/feeds/6992595527883921393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://oops-d.blogspot.com/2012/01/stl-list-remove.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/6992595527883921393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/6992595527883921393'/><link rel='alternate' type='text/html' href='http://oops-d.blogspot.com/2012/01/stl-list-remove.html' title='STL List remove()'/><author><name>Manav Prakash Sah</name><uri>http://www.blogger.com/profile/04457020170692209577</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total><georss:featurename>Mayur Vihar II, New Delhi, Delhi, India</georss:featurename><georss:point>28.620315889062766 77.299633026123047</georss:point><georss:box>28.616831389062767 77.294697526123045 28.623800389062765 77.304568526123049</georss:box></entry><entry><id>tag:blogger.com,1999:blog-5240770843138558900.post-1328840835632967866</id><published>2012-01-15T08:13:00.000-08:00</published><updated>2012-02-02T19:18:46.253-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><category scheme="http://www.blogger.com/atom/ns#" term="development"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><category scheme="http://www.blogger.com/atom/ns#" term="Published"/><category scheme="http://www.blogger.com/atom/ns#" term="software"/><title type='text'>Singleton Pattern</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Design Pattern&#39;s a tool to avoid resolving the already solved problem. Just apply the existing solution and your problem is solved. Patterns don&#39;t go directly into code, they first go into your mind. Once you train it with working knowledge, you then apply them to your design for any given problem or your old problem.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
When ever design pattern comes to you first pattern that come into mind is Singleton.&amp;nbsp;The singleton pattern deals with situations where only one instance of a class must be created.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b style=&quot;text-align: -webkit-auto;&quot;&gt;Definitions:&lt;/b&gt;&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;text-align: -webkit-auto;&quot;&gt;Ensure only one instance, and provide a global point of access to it.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;text-align: -webkit-auto;&quot;&gt;Restrict a class so that only one instance can be created.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuiBGu9onWBpyyFXU2coj9k0mhslcB3G998DoamihE207ZKn3fN5IuI_zpLu7vJLQ8SR9-TbJtqe432LRhp3zQ4P9QSC67744uqO1B0QIOtKIMBZ6i32BgjrQ113dPvGOLot64qBF-xbI/s1600/singleton_diagram.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuiBGu9onWBpyyFXU2coj9k0mhslcB3G998DoamihE207ZKn3fN5IuI_zpLu7vJLQ8SR9-TbJtqe432LRhp3zQ4P9QSC67744uqO1B0QIOtKIMBZ6i32BgjrQ113dPvGOLot64qBF-xbI/s1600/singleton_diagram.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Features:&lt;/b&gt;&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;A single instance of a subsystem shall be enforced, and the subsystem itself shall be responsible for that enforcement.&lt;/li&gt;
&lt;li&gt;The single instance shall be globally accessible.&lt;/li&gt;
&lt;li&gt;The single instance shall be initialized only if, and when, it is accessed.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
We can implement singleton class by doing following changes to any class:&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Make following private&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Constructor : to control&amp;nbsp;instantiating&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Copy Constructor : do &amp;nbsp;not want to create copies.&lt;/li&gt;
&lt;li&gt;Assignment Operator&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Create method that , creates a new instance of the class if one does not exist, return object reference.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Usages:&lt;/b&gt;&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Base for implementing many other pattern like factory, abstract factory etc.&lt;/li&gt;
&lt;li&gt;Very common use is to create logger for you application.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Problems:&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Make debugging difficult&amp;nbsp;&lt;/li&gt;
&lt;li&gt;extra care should be taken while implementing for threaded application.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;pre&gt;&lt;pre&gt;&lt;span style=&quot;color: green;&quot;&gt;/*                   Singleton.h                              */&lt;/span&gt;&lt;/pre&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy;&quot;&gt;#ifndef&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;SINGLETON_H_&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;SINGLETON_H_&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;SINGLETON&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;singleton::Singleton::getInstance()&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;public&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;virtual&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;~&lt;span style=&quot;font-style: italic;&quot;&gt;Singleton&lt;/span&gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;&amp;amp;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;getInstance();&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;doTask()&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;private&lt;/span&gt;:&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;();&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;(&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;amp;);&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;&amp;amp;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;=&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;(&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;amp;);&lt;/pre&gt;
&lt;pre&gt;};&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;}&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;*/&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy;&quot;&gt;#endif&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;SINGLETON_H_&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;*/&lt;/span&gt;&lt;/pre&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;pre&gt;&lt;span style=&quot;color: green;&quot;&gt;/*                   Singleton.cpp                              */&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;Singleton.h&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;assert.h&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;::~&lt;span style=&quot;font-style: italic;&quot;&gt;Singleton&lt;/span&gt;()&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;TODO&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;cleaning&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;::&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;()&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;TODO&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;stub&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;::&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;(&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;amp;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;ref)&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;TODO copy constructor&lt;/span&gt;    &lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;global interface&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;amp;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;::getInstance()&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;obj;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;obj;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;   &lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;amp;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;::&lt;span style=&quot;color: olive;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;=(&lt;span style=&quot;color: olive;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;amp;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;ref)&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;     &lt;/span&gt;assert(&lt;span style=&quot;color: olive;&quot;&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;     &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;*&lt;span style=&quot;color: olive;&quot;&gt;this&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;task performed by class&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;Singleton&lt;/span&gt;::doTask()&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;const &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;        &lt;/span&gt;cout&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;lt;&amp;lt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;task&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;do&quot;&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&amp;lt;&amp;lt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;endl;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;}&lt;span style=&quot;color: green;&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;singleton&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;*/&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: green;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;pre&gt;&lt;span style=&quot;color: green;&quot;&gt;/*                   main.cpp                              */&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: navy;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;&quot;Singleton.h&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;pre&gt;&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;main(&lt;span style=&quot;color: olive;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;argc,&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;*argv[])  {&lt;/pre&gt;
&lt;pre&gt;&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;SINGLETON.doTask();&lt;/pre&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: silver;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: silver;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: navy;&quot;&gt;0&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style=&quot;color: green;&quot;&gt;/****************************************************************/&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://oops-d.blogspot.com/feeds/1328840835632967866/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://oops-d.blogspot.com/2012/01/singleton-pattern.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/1328840835632967866'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5240770843138558900/posts/default/1328840835632967866'/><link rel='alternate' type='text/html' href='http://oops-d.blogspot.com/2012/01/singleton-pattern.html' title='Singleton Pattern'/><author><name>Manav Prakash Sah</name><uri>http://www.blogger.com/profile/04457020170692209577</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuiBGu9onWBpyyFXU2coj9k0mhslcB3G998DoamihE207ZKn3fN5IuI_zpLu7vJLQ8SR9-TbJtqe432LRhp3zQ4P9QSC67744uqO1B0QIOtKIMBZ6i32BgjrQ113dPvGOLot64qBF-xbI/s72-c/singleton_diagram.png" height="72" width="72"/><thr:total>0</thr:total></entry></feed>