亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ch26.htm

?? Learning language of Visual C++6
?? HTM
?? 第 1 頁 / 共 3 頁
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
<SCRIPT LANGUAGE="JavaScript">

<!--

function popUp(pPage) {
 var fullURL = document.location;
 var textURL = fullURL.toString();
 var URLlen = textURL.length;
 var lenMinusPage = textURL.lastIndexOf("/");
 lenMinusPage += 1;
 var fullPath = textURL.substring(0,lenMinusPage);
 popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
 figDoc= popUpWin.document;
 zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
 zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
 zhtm += '<BODY bgcolor="#FFFFFF">';
 zhtm += '<IMG SRC="' + fullPath + pPage + '">';
 zhtm += '<P><B>' + pPage + '</B>';
 zhtm += '</BODY></HTML>';
 window.popUpWin.document.write(zhtm);
 window.popUpWin.document.close();
 // Johnny Jackson 4/28/98
 }

//-->
                                                                
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">

	
	<TITLE>Special Edition Using Visual C++ 6 -- Ch 26 -- Exceptions and Templates</TITLE>
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF">

<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch25/ch25.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch27/ch27.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> 
<HR>

</CENTER>
<CENTER>
<H1>- 26 -</H1>

<H1>Exceptions and Templates</H1>
</CENTER>

<UL>
	<LI><A HREF="#Heading1">Understanding Exceptions</A>
	<UL>
		<LI><A HREF="#Heading2">Simple Exception Handling</A>
		<LI><A HREF="#Heading3">Exception Objects</A>
		<LI><A HREF="#Heading4">Placing the catch Block</A>
		<LI><A HREF="#Heading5">Handling Multiple Types of Exceptions</A>
		<LI><A HREF="#Heading6">The Old Exception Mechanism</A>
	</UL>
	<LI><A HREF="#Heading7">Exploring Templates</A>
	<UL>
		<LI><A HREF="#Heading8">Introducing Templates</A>
		<LI><A HREF="#Heading9">Creating Function Templates</A>
		<LI><A HREF="#Heading10">Creating Class Templates</A>
	</UL>
	<LI><A HREF="#Heading11">The Standard Template Library</A>
	<UL>
		<LI><A HREF="#Heading12">Managed Pointer Templates: auto_ptr</A>
		<LI><A HREF="#Heading13">Other Useful STL Templates</A>
	</UL>
	<LI><A HREF="#Heading14">Understanding Namespaces</A>
	<UL>
		<LI><A HREF="#Heading15">Defining a Namespace</A>
		<LI><A HREF="#Heading16">Namespace Scope Resolution</A>
		<LI><A HREF="#Heading17">Unnamed Namespaces</A>
		<LI><A HREF="#Heading18">Namespace Aliases</A>
	</UL>
</UL>

<P>
<HR SIZE="4">

<CENTER>
<H1></H1>
</CENTER>
<P>C++ is an evolving language and frequently undergoes review and improvement. Two
important features that were added to C++ after many developers had already learned
the language are exceptions and templates. Although most programmers delay learning
these concepts until they have six months to a year of Visual C++ programming experience,
you should consider learning them now. These concepts are not much more difficult
than the ones covered earlier in this book and can add extra power to your programs.</P>
<P>
<H2><A NAME="Heading1"></A>Understanding Exceptions</H2>
<P>When writing applications using Visual C++, sooner or later you're going to run
into error-handling situations that don't seem to have a solution. Perhaps you are
writing a function that returns a numeric value and need a way to send back an error
response. Sometimes you can come up with one special return value, perhaps 0 or -1,
that indicates a problem. Other times there doesn't seem to be a way to signal trouble.
Perhaps you use special return values but find yourself writing code that starts
out like this:</P>
<P>
<PRE>while (somefunction(x))
{
    for (int i=0; i&lt;limit; i++)
    {
        y = someotherfunction(i);
    }
}
</PRE>
<P>After writing that, perhaps you realize that if someotherfunction() returns -1,
you should not move on to the next i, and you should leave the while loop. Your code
becomes the following:</P>
<P>
<PRE>int timetostop = 0;
while (somefunction(x) &amp;&amp; !timetostop)
{
    for (int i=0; i&lt;limit &amp;&amp; !timetostop; i++)
    {
        if ( (y = someotherfunction(i)) == -1)
            timetostop = 1;
    }
}
</PRE>
<P>This isn't bad, but it is hard to read. If there are two or three things that
could go wrong, your code becomes unmanageably complex.</P>
<P>Exceptions are designed to handle these sorts of problems. The exception mechanism
allows programs to signal each other about serious and unexpected problems. Three
places in your code participate in most exceptions:</P>
<P>

<UL>
	<LI>The try<I> block</I> marks the code you believe might run into difficulty.
	<P>
	<LI>The catch<I> block</I> immediately follows the try block and holds the code that
	deals with the problem.
	<P>
	<LI>The throw<I> statement</I> is how the code with a problem notifies the calling
	code.
</UL>

<H3><A NAME="Heading2"></A>Simple Exception Handling</H3>
<P>The mechanism used by exception-handling code is simple. Place the source code
that you want guarded against errors inside a try block. Then construct a catch program
block that acts as the error handler. If the code in the try block (or any code called
from the try block) throws an exception, the try block immediately ceases execution,
and the program continues inside the catch block.</P>
<P>For example, memory allocation is one place in a program where you might expect
to run into trouble. Listing 26.1 shows a nonsensical little program that allocates
some memory and then immediately deletes it. Because memory allocation could fail,
the code that allocates the memory is enclosed in a try program block. If the pointer
returned from the memory allocation is NULL, the try block throws an exception. In
this case, the exception object is a string.</P>


<BLOCKQUOTE>
	<P>
<HR>
<strong>NOTE:</strong> The sample applications in this chapter are console applications,
	which can run from a DOS prompt and don't have a graphical interface. This keeps
	them small enough to be shown in their entirety in the listings. To try them, create
	a console application as discussed in Chapter 28, &quot;Future Explorations,&quot;
	add a file to the project, and add the code shown here.&#160;
<HR>


</BLOCKQUOTE>

<H4>Listing 26.1&#160;&#160;EXCEPTION1.CPP--Simple Exception Handling</H4>
<PRE>#include &lt;iostream.h&gt;
int main()
{
    int* buffer;
    try
    {
        buffer = new int[256];
        if (buffer == NULL)
            throw &quot;Memory allocation failed!&quot;;
        else
            delete buffer;
    }
    catch(char* exception)
    {
        cout &lt;&lt; exception &lt;&lt; endl;
    }
    return 0;
</PRE>
<PRE>}
</PRE>
<P>When the program throws the exception, program execution jumps to the first line
of the catch program block. (The remainder of the code inside the try block is not
executed.) In the case of Listing 26.1, this line prints out a message, after which
the function's return line is executed and the program ends.</P>
<P>If the memory allocation is successful, the program executes the entire try block,
deleting the buffer. Then program execution skips over the catch block completely,
in this case going directly to the return statement.</P>


<BLOCKQUOTE>
	<P>
<HR>
<strong>NOTE:</strong> The catch program block does more than direct program execution.
	It actually catches the exception object thrown by the program. For example, in Listing
	26.1, you can see the exception object being caught inside the parentheses following
	the catch keyword. This is very similar to a parameter being received by a method.
	In this case, the type of the &quot;parameter&quot; is char* and the name of the
	parameter is exception.&#160;
<HR>


</BLOCKQUOTE>

<H3><A NAME="Heading3"></A>Exception Objects</H3>
<P>The beauty of C++ exceptions is that the exception object thrown can be just about
any kind of data structure you like. For example, you might want to create an exception
class for certain kinds of exceptions that occur in your programs. Listing 26.2 shows
a program that defines a general-purpose exception class called MyException. In the
case of a memory-allocation failure, the main program creates an object of the class
and throws it. The catch block catches the MyException object, calls the object's
GetError() member function to get the object's error string, and then displays the
string on the screen.</P>
<P>
<H4>Listing 26.2&#160;&#160;EXCEPTION2.CPP--Creating an Exception Class</H4>
<PRE>#include &lt;iostream.h&gt;
class MyException
{
protected:
    char* m_msg;
public:
    MyException(char *msg) { m_msg = msg; }
    ~MyException(){}
    char* GetError() {return m_msg; };
};
int main()
{
    int* buffer;
    
    try
    {
        buffer = new int[256];
        if (buffer == NULL)
        {
            MyException* exception =
                new MyException(&quot;Memory allocation failed!&quot;);
            throw exception;
        }
        else
            delete buffer;
    }
    catch(MyException* exception)
    {
        char* msg = exception-&gt;GetError();
        cout &lt;&lt; msg &lt;&lt; endl;
    }
    return 0;
</PRE>
<PRE>}
</PRE>
<P>An exception object can be as simple as an integer error code or as complex as
a fully developed class. MFC provides a number of exception classes, including CException
and several classes derived from it. The abstract class CException has a constructor
and three member functions: Delete(), which deletes the exception, GetErrorMessage(),
which returns a string describing the exception, and ReportError(), which reports
the error in a message box.</P>
<P>
<H3><A NAME="Heading4"></A>Placing the catch Block</H3>
<P>The catch program block doesn't have to be in the same function as the one in
which the exception is thrown. When an exception is thrown, the system starts &quot;unwinding
the stack,&quot; looking for the nearest catch block. If the catch block is not found
in the function that threw the exception, the system looks in the function that called
the throwing function. This search continues up the function-call stack. If the exception
is never caught, the program halts.</P>
<P>Listing 26.3 is a short program that demonstrates this concept. The program throws
the exception from the AllocateBuffer() function but catches the exception in main(),
which is the function from which AllocateBuffer() is called.</P>
<P>
<H4>Listing 26.3&#160;&#160;EXCEPTION3.CPP--Catching Exceptions Outside the <BR>
Throwing Function</H4>
<PRE>#include &lt;iostream.h&gt;
class MyException
{
protected:
    char* m_msg;
public:
    MyException(char *msg) { m_msg = msg;}
    ~MyException(){}
    char* GetError() {return m_msg;}
};
class BigObject
{
private:
     int* intarray;
public:
     BigObject() {intarray = new int[1000];}
     ~BigObject() {delete intarray;}
};
int* AllocateBuffer();
int main()
{
    int* buffer;
    
    try
    {
        buffer = AllocateBuffer();
        delete buffer;
    }
    catch (MyException* exception)
    {
        char* msg = exception-&gt;GetError();
        cout &lt;&lt; msg &lt;&lt; endl;
    }
    return 0;
}
int* AllocateBuffer()
{
    BigObject bigarray;
    float* floatarray = new float[1000];
    int* buffer = new int[256];
    if (buffer == NULL)
    {
        MyException* exception =
            new MyException(&quot;Memory allocation failed!&quot;);
        throw exception;
    }
    delete floatarray;
    return buffer;
</PRE>
<PRE>}
</PRE>
<P>When the exception is thrown in AllocateBuffer(), the remainder of the function
is not executed. The dynamically allocated floatarray will not be deleted. The BigObject
that was allocated on the stack will go out of scope, and its destructor will be
executed, deleting the intarray member variable that was allocated with new in the
constructor. This is an important concept to grasp: Objects created on the stack
will be destructed as the stack unwinds. Objects created on the heap will not. Your
code must take care of these. For example, AllocateBuffer() should include code to
delete floatarray before throwing the exception, like this:</P>
<P>
<PRE>if (buffer == NULL)
    {
        MyException* exception =
            new MyException(&quot;Memory allocation failed!&quot;);
        delete floatarray;
        throw exception;
    }
</PRE>
<P>In many cases, using an object with a carefully written destructor can save significant
code duplication when you are using exceptions. If you are using objects allocated
on the heap, you may need to catch and rethrow exceptions so that you can delete
them. Consider the code in Listing 26.4, in which the exception is thrown right past
an intermediate function up to the catching function.</P>
<P>
<H4>Listing 26.4&#160;&#160;EXCEPTION4.CPP--Unwinding the Stack</H4>
<PRE>#include &lt;iostream.h&gt;
class MyException
{
protected:
    char* m_msg;
public:
    MyException(char *msg) { m_msg = msg;}
    ~MyException(){}
    char* GetError() {return m_msg;}
};
class BigObject
{
private:
     int* intarray;
public:
     BigObject() {intarray = new int[1000];} 
     ~BigObject() {delete intarray;}
};
int* AllocateBuffer();
int* Intermediate();
int main()
{
    int* buffer;
    
    try
    {
        buffer = Intermediate();
        delete buffer;
    }
    catch (MyException* exception)
    {
        char* msg = exception-&gt;GetError();
        cout &lt;&lt; msg &lt;&lt; endl;
    }
    return 0;
}
int* Intermediate()
{
    BigObject bigarray;
    float* floatarray = new float[1000];

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久精品国产久精国产| 一区二区三区在线观看欧美| 日韩一区二区三区在线| 51精品国自产在线| 91精品国产综合久久精品图片| 欧美日韩午夜精品| 欧美一区午夜精品| 日韩欧美高清一区| 久久五月婷婷丁香社区| 中文一区一区三区高中清不卡| 中文字幕精品一区二区精品绿巨人| 欧美国产日韩一二三区| 国产精品家庭影院| 亚洲综合成人网| 日产国产高清一区二区三区| 精品一区二区在线观看| 国产成人精品一区二区三区四区 | 国产精品嫩草影院com| 国产精品视频你懂的| 亚洲三级在线看| 天天综合网 天天综合色| 精品亚洲国产成人av制服丝袜| 粉嫩高潮美女一区二区三区| 一本到一区二区三区| 在线播放中文字幕一区| 久久久久久久久免费| 亚洲欧美一区二区三区极速播放| 日韩va欧美va亚洲va久久| 国产高清在线观看免费不卡| 91久久精品日日躁夜夜躁欧美| 日韩午夜激情免费电影| 国产精品久久一级| 日本亚洲天堂网| www.亚洲精品| 精品国产成人在线影院| 亚洲日本va午夜在线影院| 麻豆久久一区二区| 色噜噜久久综合| 国产亚洲一区二区三区在线观看| 亚洲综合一二区| 成人午夜视频免费看| 日韩一区二区不卡| 亚洲一区二区三区三| 国产精品亚洲专一区二区三区 | 免费美女久久99| 9l国产精品久久久久麻豆| 欧美一二三在线| 亚洲男人天堂一区| 国产精品一二三四| 日韩一区二区影院| 亚洲国产视频直播| 91在线视频18| 国产精品日韩精品欧美在线| 蜜臀91精品一区二区三区| 在线观看91视频| 亚洲视频每日更新| 成人性色生活片| 久久蜜桃一区二区| 裸体歌舞表演一区二区| 9191久久久久久久久久久| 亚洲色图视频网| 成人av在线播放网站| 久久精品人人爽人人爽| 国产一区二区视频在线| 日韩一区二区三区四区五区六区| 性感美女久久精品| 欧美日韩精品一区二区三区蜜桃 | 成人激情视频网站| 国产欧美一区二区三区在线看蜜臀| 日本欧美一区二区在线观看| 欧美无乱码久久久免费午夜一区 | 亚洲一区二区三区四区的| 99精品在线免费| 中文字幕一区二区三| 99久久伊人网影院| 国产精品成人一区二区艾草| jlzzjlzz亚洲女人18| 国产精品久久久久影院亚瑟| 99久久99久久久精品齐齐| 亚洲乱码国产乱码精品精的特点| av网站免费线看精品| 中文字幕一区二区三区色视频| 91无套直看片红桃| 亚洲一区二区高清| 欧美人体做爰大胆视频| 理论片日本一区| 久久久久九九视频| 91丨九色丨尤物| 亚洲国产aⅴ成人精品无吗| 91精品国产欧美一区二区成人| 久久精品国产99国产| 国产欧美精品一区二区色综合| 粉嫩一区二区三区性色av| 综合激情成人伊人| 91麻豆精品国产自产在线观看一区| 麻豆精品新av中文字幕| 欧美激情一区二区三区蜜桃视频 | 欧美少妇bbb| 奇米888四色在线精品| 久久婷婷色综合| 99精品国产热久久91蜜凸| 亚洲一二三四在线| 26uuu欧美日本| 99久久精品国产网站| 日韩av中文字幕一区二区三区| 久久久久久麻豆| 在线观看国产一区二区| 久国产精品韩国三级视频| 国产精品久久久久久久久快鸭| 欧美性生活久久| 国产精品一区二区在线观看网站| 亚洲女女做受ⅹxx高潮| 欧美成人a∨高清免费观看| 不卡av在线网| 久久成人羞羞网站| 亚洲综合999| 国产精品久久久久久久久免费丝袜| 欧美喷潮久久久xxxxx| 99久久久久久| 国产一区二区看久久| 亚洲国产人成综合网站| 国产精品毛片高清在线完整版| 91麻豆精品国产91久久久| 99精品黄色片免费大全| 国产成人在线视频网站| 日韩电影在线一区二区三区| 亚洲欧洲日产国码二区| 久久久久久亚洲综合| 欧美一区二区三区色| 91美女在线看| 成人黄色电影在线| 久久国产精品一区二区| 午夜精品福利一区二区三区蜜桃| 中文字幕日韩欧美一区二区三区| wwwwxxxxx欧美| 欧美一区二区三区啪啪| 精品婷婷伊人一区三区三| av激情亚洲男人天堂| 成人免费高清视频在线观看| 国产在线精品视频| 久久99热这里只有精品| 免费在线看一区| 蜜臀99久久精品久久久久久软件| 亚洲一区二区三区四区五区中文| 亚洲美腿欧美偷拍| 亚洲精品中文在线影院| 亚洲日本va在线观看| ...xxx性欧美| 亚洲日本乱码在线观看| 亚洲欧洲制服丝袜| 亚洲一区二区三区美女| 亚洲第一主播视频| 日韩av电影天堂| 精品一区二区在线视频| 国产在线不卡一卡二卡三卡四卡| 国内一区二区视频| 国产盗摄女厕一区二区三区| 国产一区二区三区免费| 国产精品一二三四五| 成人午夜私人影院| 色综合久久综合| 欧美日韩精品三区| 欧美成人video| 国产欧美日韩三区| 亚洲欧洲精品一区二区三区| 亚洲免费av观看| 亚洲成人三级小说| 美女视频黄免费的久久| 成人性色生活片| 在线看不卡av| 欧美一级片在线| 久久影院电视剧免费观看| 国产精品麻豆一区二区| 亚洲午夜久久久久久久久久久 | 国产综合色视频| 菠萝蜜视频在线观看一区| 欧美亚洲综合网| 日韩美女在线视频| 国产精品国产馆在线真实露脸| 玉足女爽爽91| 免费久久精品视频| av一二三不卡影片| 欧美高清精品3d| 欧美国产亚洲另类动漫| 五月婷婷激情综合| 国产成人一级电影| 欧美日韩另类一区| 国产农村妇女精品| 污片在线观看一区二区| 成人性生交大片免费| 日韩欧美中文字幕制服| 亚洲丝袜另类动漫二区| 韩国精品主播一区二区在线观看| 日本道免费精品一区二区三区| 欧美xxx久久| 同产精品九九九| 91视频.com| 久久精品在线免费观看| 日韩av一区二| 欧美性色黄大片手机版|