📄 atl under the hood - part 1.mht
字号:
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
src=3D"http://www.codeproject.com/images/print.gif" =
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/atl/atl_underthehood_.asp?print=3Dtrue=
"=20
target=3D_print>Print version</A></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
src=3D"http://www.codeproject.com/images/link.gif" =
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/script/profile/add_bookmark.asp?t=3D0&=
amp;ct=3D%2Fatl%2Fatl%5Funderthehood%5F%2Easp&guid=3D%2Fatl%2FATL%5FU=
nderTheHood%5F%2Easp%2D1%2F28%2F2002">Bookmark=20
this</A></TD></TR></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
src=3D"http://www.codeproject.com/images/mail.gif" =
width=3D16></TD>
<TD class=3DSmallText><A=20
=
href=3D"http://www.codeproject.com/script/recommend/form.asp?guid=3D%2Fat=
l%2FATL%5FUnderTheHood%5F%2Easp%2D1%2F28%2F2002">Send=20
to a friend</A></TD></TR>
<TR vAlign=3Dtop>
<TD></TD>
<TD></TD></TR>
<TR vAlign=3Dtop>
<TD></TD>
<TD class=3DInfoBarHeader>My Profile</TD></TR>
<TR>
<TD bgColor=3Dblack colSpan=3D2><IMG height=3D1=20
src=3D"http://www.codeproject.com/images/space.gif"=20
width=3D1></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
=
src=3D"http://www.codeproject.com/images/profile_small.gif"=20
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/script/profile/modify.asp?ct=3D/atl/at=
l_underthehood_.asp">My=20
Settings</A></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
=
src=3D"http://www.codeproject.com/images/submit_small.gif"=20
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/script/articles/list_articles.asp?user=
id=3D273403">My=20
Articles</A></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
=
src=3D"http://www.codeproject.com/images/bookmarks.gif"=20
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/script/profile/bookmark_list.asp">My=20
Bookmarks</A></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
=
src=3D"http://www.codeproject.com/images/logoff_small.gif"=20
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/script/profile/logoff.asp?ct=3D/atl/at=
l_underthehood_.asp">Sign=20
out</A></TD></TR>
<TR>
<TD bgColor=3Dblack colSpan=3D2><IMG height=3D1=20
src=3D"http://www.codeproject.com/images/space.gif"=20
width=3D1></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD> </TD></TR>
<TR>
<TD>
<CENTER><A=20
=
href=3D"http://www.codeproject.com/script/admentor/admentorredir.asp?id=3D=
171&way=3Dban"=20
target=3D_blank><IMG height=3D600=20
alt=3D"MSDN Magazine - Your guide to Microsoft tools, =
development environments, and technologies for Windows and the Web."=20
=
src=3D"http://www.codeproject.com/script/admentor/images/mm120_600_codepr=
ot3.gif"=20
width=3D120 =
border=3D0></A><BR></CENTER><BR></TD></TR></TBODY></TABLE></TD>
<TD width=3D5 rowSpan=3D2><IMG height=3D1=20
src=3D"http://www.codeproject.com/images/space.gif" width=3D5> =
</TD>
<TD align=3Dright width=3D"100%" bgColor=3Dwhite>
<TABLE width=3D"100%">
<TBODY>
<TR>
<TD class=3DsmallText vAlign=3Dtop noWrap></TD>
<TD noWrap align=3Dright><A name=3D__top></A>
<TABLE>
<TBODY>
<TR>
<TD class=3DsmallText align=3Dright>60 members have =
rated this=20
article. Result:</TD>
<TD>
<TABLE cellSpacing=3D0 cellPadding=3D0 border=3D2>
<TBODY>
<TR>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D15 border=3D0><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/white.gif"=20
width=3D5 =
border=3D0></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=3DsmallText align=3Dright colSpan=3D2><A=20
title=3D"Calculated as rating x Log10(# votes)"=20
=
href=3D"http://www.codeproject.com/script/articles/top_articles.asp?st=3D=
2">Popularity:=20
8.49</A>. Rating: <B>4.77</B> out of=20
5.</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD vAlign=3Dtop width=3D"100%">
<DIV id=3Dcontentdiv><!-- Article Starts --><!-- Add the rest of =
your HTML here -->
<H2>Introduction</H2>
<P>In this series of tutorials I am going to discuss some of the =
inner=20
workings of ATL and the techniques that ATL uses.=20
<P>Let's start the discussion by talking about the memory layout =
of a=20
program. Let's make a simple program which doesn't have any data =
members=20
and take a look at the memory structure of it.</P>
<H3>Program 1.</H3><PRE><SPAN class=3Dcpp-preprocessor>#include =
<iostream></SPAN>
using <SPAN class=3Dcpp-keyword>namespace</SPAN> std;
<SPAN class=3Dcpp-keyword>class</SPAN> Class {
};
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
Class objClass;
cout << "Size of object is =3D " << <SPAN =
class=3Dcpp-keyword>sizeof</SPAN>(objClass) << endl;
cout << "Address of object is =3D " << &objClass =
<< endl;
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
</PRE>
<P>The output of this program is </P><PRE>Size of object is =3D =
<SPAN class=3Dcpp-literal>1</SPAN>
Address of object is =3D 0012FF7C</PRE>
<P>Now if we are going to add some data members then the size of =
the class=20
is the sum of all the storage of the individual member variables. =
It is=20
also true in the case of template. Now let's take a look at =
template class=20
of Point.=20
<H3>Program 2.</H3><PRE><SPAN class=3Dcpp-preprocessor>#include =
<iostream></SPAN>
using <SPAN class=3Dcpp-keyword>namespace</SPAN> std;
<SPAN class=3Dcpp-keyword>template</SPAN> <<SPAN =
class=3Dcpp-keyword>typename</SPAN> T>
<SPAN class=3Dcpp-keyword>class</SPAN> CPoint {
<SPAN class=3Dcpp-keyword>public</SPAN>:
T m_x;
T m_y;
};
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
CPoint<<SPAN class=3Dcpp-keyword>int</SPAN>> objPoint;
cout << "Size of object is =3D " << <SPAN =
class=3Dcpp-keyword>sizeof</SPAN>(objPoint) << endl;
cout << "Address of object is =3D " << &objPoint =
<< endl;
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
</PRE>
<P>Now the output of the program is <BR></P><PRE>Size of object is =
=3D <SPAN class=3Dcpp-literal>8</SPAN>
Address of object is =3D 0012FF78
</PRE>
<P>Now add inheritance too in the program. Now we are going to =
inherit=20
class Point3D from Point class and see the memory structure of =
this=20
program.=20
<H3>Program 3.</H3><PRE><SPAN class=3Dcpp-preprocessor>#include =
<iostream></SPAN>
using <SPAN class=3Dcpp-keyword>namespace</SPAN> std;
<SPAN class=3Dcpp-keyword>template</SPAN> <<SPAN =
class=3Dcpp-keyword>typename</SPAN> T>
<SPAN class=3Dcpp-keyword>class</SPAN> CPoint {
<SPAN class=3Dcpp-keyword>public</SPAN>:
T m_x;
T m_y;
};
<SPAN class=3Dcpp-keyword>template</SPAN> <<SPAN =
class=3Dcpp-keyword>typename</SPAN> T>
<SPAN class=3Dcpp-keyword>class</SPAN> CPoint3D : <SPAN =
class=3Dcpp-keyword>public</SPAN> CPoint<T> {
<SPAN class=3Dcpp-keyword>public</SPAN>:
T m_z;
};
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
CPoint<<SPAN class=3Dcpp-keyword>int</SPAN>> objPoint;
cout << "Size of object Point is =3D " << <SPAN =
class=3Dcpp-keyword>sizeof</SPAN>(objPoint) << endl;
cout << "Address of object Point is =3D " << &objPoint =
<< endl;
CPoint3D<<SPAN class=3Dcpp-keyword>int</SPAN>> objPoint3D;
cout << "Size of object Point3D is =3D " << <SPAN =
class=3Dcpp-keyword>sizeof</SPAN>(objPoint3D) << endl;
cout << "Address of object Point3D is =3D " << =
&objPoint3D << endl;
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}</PRE>
<P>The output of this program is </P><PRE>Size of object Point is =
=3D <SPAN class=3Dcpp-literal>8</SPAN>
Address of object Point is =3D 0012FF78
Size of object Point3D is =3D <SPAN class=3Dcpp-literal>12</SPAN>
Address of object Point3D is =3D 0012FF6C</PRE>This program shows the =
memory=20
structure of the derived class. It shows the memory occupied by =
the object=20
is sum of its data member plus its base member. =20
<P>Things become interesting when a virtual function joins the =
party. Take=20
a look at the following program</P>
<H3>Program 4.</H3><PRE><SPAN class=3Dcpp-preprocessor>#include =
<iostream></SPAN>
using <SPAN class=3Dcpp-keyword>namespace</SPAN> std;
<SPAN class=3Dcpp-keyword>class</SPAN> Class {
<SPAN class=3Dcpp-keyword>public</SPAN>:
<SPAN class=3Dcpp-keyword>virtual</SPAN> <SPAN =
class=3Dcpp-keyword>void</SPAN> fun() { cout << "Class::fun" =
<< endl; }
};
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
Class objClass;
cout << "Size of Class =3D " << <SPAN =
class=3Dcpp-keyword>sizeof</SPAN>(objClass) << endl;
cout << "Address of Class =3D " << &objClass << =
endl;
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}</PRE>
<P>The output of the program is </P><PRE>Size of Class =3D <SPAN =
class=3Dcpp-literal>4</SPAN>
Address of Class =3D 0012FF7C</PRE>
<P>And situation becomes more interesting when we add more than =
one=20
virtual function.=20
<H3>Program 5.</H3><PRE><SPAN class=3Dcpp-preprocessor>#include =
<iostream></SPAN>
using <SPAN class=3Dcpp-keyword>namespace</SPAN> std;
<SPAN class=3Dcpp-keyword>class</SPAN> Class {
<SPAN class=3Dcpp-keyword>public</SPAN>:
<SPAN class=3Dcpp-keyword>virtual</SPAN> <SPAN =
class=3Dcpp-keyword>void</SPAN> fun1() { cout << "Class::fun1" =
<< endl; }
<SPAN class=3Dcpp-keyword>virtual</SPAN> <SPAN =
class=3Dcpp-keyword>void</SPAN> fun2() { cout << "Class::fun2" =
<< endl; }
<SPAN class=3Dcpp-keyword>virtual</SPAN> <SPAN =
class=3Dcpp-keyword>void</SPAN> fun3() { cout << "Class::fun3" =
<< endl; }
};
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
Class objClass;
cout << "Size of Class =3D " << <SPAN =
class=3Dcpp-keyword>sizeof</SPAN>(objClass) << endl;
cout << "Address of Class =3D " << &objClass << =
endl;
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}</PRE>
<P>The output of the program is same as above program. Let's do =
one more=20
experiment to better understand it.=20
<H3>Program 6.</H3><PRE><SPAN class=3Dcpp-preprocessor>#include =
<iostream></SPAN>
using <SPAN class=3Dcpp-keyword>namespace</SPAN> std;
<SPAN class=3Dcpp-keyword>class</SPAN> CPoint {
<SPAN class=3Dcpp-keyword>public</SPAN>:
<SPAN class=3Dcpp-keyword>int</SPAN> m_ix;
<SPAN class=3Dcpp-keyword>int</SPAN> m_iy;
<SPAN class=3Dcpp-keyword>virtual</SPAN> ~CPoint() { };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -