﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Mr.Johnson is Here</title><link>http://www.cppblog.com/koople/</link><description>有时候我会相信一切都会有尽头</description><language>zh-cn</language><lastBuildDate>Tue, 07 Apr 2026 20:17:33 GMT</lastBuildDate><pubDate>Tue, 07 Apr 2026 20:17:33 GMT</pubDate><ttl>60</ttl><item><title>error C2632: 'long' followed by 'long' is illegal</title><link>http://www.cppblog.com/koople/archive/2011/09/05/155148.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Mon, 05 Sep 2011 10:40:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2011/09/05/155148.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/155148.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2011/09/05/155148.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/155148.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/155148.html</trackback:ping><description><![CDATA[&nbsp; &nbsp;今天在编译模拟器的时候，移植一段代码，发现怎么都编译不过，总是报c:\program files\microsoft visual studio\vc98\include\windef.h(141) : error C2632: 'long' followed by 'long' is illegal 错误。<br />&nbsp; &nbsp;发现是&nbsp;<span style="font-family: simsun; line-height: 23px; text-align: left; background-color: #ffffff; ">头文件</span><span style="font-family: simsun; line-height: 23px; text-align: left; background-color: #ffffff; ">可能最后少},; #endif 之类的东西。</span><img src ="http://www.cppblog.com/koople/aggbug/155148.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2011-09-05 18:40 <a href="http://www.cppblog.com/koople/archive/2011/09/05/155148.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>有关于internal linkage 和 external linkage</title><link>http://www.cppblog.com/koople/archive/2011/04/01/143220.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Fri, 01 Apr 2011 09:58:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2011/04/01/143220.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/143220.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2011/04/01/143220.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/143220.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/143220.html</trackback:ping><description><![CDATA[<span class=Apple-style-span style="WORD-SPACING: 0px; FONT: medium Simsun; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; orphans: 2; widows: 2">&nbsp;&nbsp;&nbsp;&nbsp;今天看《STL templates：The Complete Guide》看到有关于Restrictions for Nontype Template Parameters 部分，提出了一个有关于external linkage概念，网上一搜基本上都是同一篇中文的有关于internal linkage 和 external linkage的解释，看了半天我还是有点迷糊，用google搜到一本书叫做The C Book 的书里面有有关于这个概念的介绍。<br>&nbsp;&nbsp;&nbsp; 在C语言中有两种linkage object:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.external object<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; external object就是函数外的声明。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.internal object<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;internal object就是函数内的声明。<br><br>&nbsp;&nbsp;&nbsp; 在C语言中有三种linkage：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.external linkage<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.internal linkage<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.none linkage<br>&nbsp;&nbsp;&nbsp; 有关于external linkage 就是所谓的全局变量作用域，internal linkage就是只能在单个文件内使用的变量的作用域，而none linkage 就是那些函数的参数作用域。<br>
<table style="WIDTH: 320px; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=3 align=center border=1>
    <tbody>
        <tr align=middle>
            <td style="WIDTH: 88px; HEIGHT: 44px; BACKGROUND-COLOR: #00ffff"><strong>Type of linkage</strong></td>
            <td><strong>Type of object</strong></td>
            <td><span class=Apple-style-span style="WORD-SPACING: 0px; FONT: medium Simsun; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; orphans: 2; widows: 2"><span class=Apple-style-span style="FONT-WEIGHT: bold; FONT-FAMILY: Arial; BORDER-COLLAPSE: collapse; TEXT-ALIGN: left; webkit-border-horizontal-spacing: 2px; webkit-border-vertical-spacing: 2px">Accessibility</span></span></td>
        </tr>
        <tr>
            <td>external</td>
            <td>external</td>
            <td>throughout the program</td>
        </tr>
        <tr>
            <td>internal</td>
            <td>external</td>
            <td>a single file</td>
        </tr>
        <tr>
            <td>none</td>
            <td>internal</td>
            <td>local to a single function</td>
        </tr>
    </tbody>
</table>
<br></span>
<img src ="http://www.cppblog.com/koople/aggbug/143220.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2011-04-01 17:58 <a href="http://www.cppblog.com/koople/archive/2011/04/01/143220.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>有关time的函数</title><link>http://www.cppblog.com/koople/archive/2011/02/11/139882.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Fri, 11 Feb 2011 02:00:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2011/02/11/139882.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/139882.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2011/02/11/139882.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/139882.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/139882.html</trackback:ping><description><![CDATA[时间日期函数,函数库为time.h<br>在时间日期函数里,主要用到的结构有以下几个:<br>总时间日期贮存结构tm<br>┌──────────────────────┐<br>│struct tm                                   │<br>│{                                           │<br>│ int tm_sec;   /*秒,0-59*/                  │<br>│ int tm_min;   /*分,0-59*/                  │<br>│ int tm_hour;  /*时,0-23*/                  │<br>│ int tm_mday;  /*天数,1-31*/                │<br>│ int tm_mon;   /*月数,0-11*/                │<br>│ int tm_year;  /*自1900的年数*/             │<br>│ int tm_wday;  /*自星期日的天数0-6*/        │<br>│ int tm_yday;  /*自1月1日起的天数,0-365*/   │<br>│ int tm_isdst; /*是否采用夏时制,采用为正数*/│<br>│}                                           │<br>└──────────────────────┘<br>日期贮存结构date<br>┌───────────────┐<br>│struct date                   │<br>│{                             │<br>│ int da_year; /*自1900的年数*/│<br>│ char da_day; /*天数*/        │<br>│ char da_mon; /*月数 1=Jan*/  │<br>│}                             │<br>└───────────────┘<br>时间贮存结构time<br>┌────────────────┐<br>│struct time                     │<br>│{                               │<br>│ unsigned char ti_min;  /*分钟*/│<br>│ unsigned char ti_hour; /*小时*/│<br>│ unsigned char ti_hund;         │<br>│ unsigned char ti_sec;  /*秒*/  │<br>│}                               │<br>└────────────────┘<br>char      *ctime(long *clock)<br> &nbsp;           本函数把clock所指的时间(如由函数time返回的时间)转换成下列格式的<br> &nbsp;           字符串:Mon Nov 21 11:31:54 1983\n\0<br>char      *asctime(struct tm *tm)<br> &nbsp;           本函数把指定的tm结构类的时间转换成下列格式的字符串:<br> &nbsp;           Mon Nov 21 11:31:54 1983\n\0<br>double     difftime(time_t time2,time_t time1)<br> &nbsp;           计算结构time2和time1之间的时间差距(以秒为单位)<br>struct tm *gmtime(long *clock)本函数把clock所指的时间(如由函数time返回的时间)<br> &nbsp;           转换成格林威治时间,并以tm结构形式返回<br>struct tm *localtime(long *clock)本函数把clock所指的时间(如函数time返回的时间)<br> &nbsp;           转换成当地标准时间,并以tm结构形式返回<br>void       tzset()本函数提供了对UNIX操作系统的兼容性<br>long       dostounix(struct date *dateptr,struct time *timeptr)<br> &nbsp;           本函数将dateptr所指的日期,timeptr所指的时间转换成UNIX格式,并返回<br> &nbsp;           自格林威治时间1970年1月1日凌晨起到现在的秒数<br>void       unixtodos(long utime,struct date *dateptr,struct time *timeptr)<br> &nbsp;           本函数将自格林威治时间1970年1月1日凌晨起到现在的秒数utime转换成<br> &nbsp;           DOS格式并保存于用户所指的结构dateptr和timeptr中<br>void       getdate(struct date *dateblk)本函数将计算机内的日期写入结构dateblk<br> &nbsp;           中以供用户使用<br>void       setdate(struct date *dateblk)本函数将计算机内的日期改成<br> &nbsp;           由结构dateblk所指定的日期<br>void       gettime(struct time *timep)本函数将计算机内的时间写入结构timep中,<br> &nbsp;           以供用户使用<br>void       settime(struct time *timep)本函数将计算机内的时间改为<br> &nbsp;           由结构timep所指的时间<br>long       time(long *tloc)本函数给出自格林威治时间1970年1月1日凌晨至现在所经<br> &nbsp;           过的秒数,并将该值存于tloc所指的单元中.<br>int        stime(long *tp)本函数将tp所指的时间(例如由time所返回的时间)<br> &nbsp;           写入计算机中.<img src ="http://www.cppblog.com/koople/aggbug/139882.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2011-02-11 10:00 <a href="http://www.cppblog.com/koople/archive/2011/02/11/139882.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++ template 不错的教程 （转载）</title><link>http://www.cppblog.com/koople/archive/2010/12/12/136195.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Sat, 11 Dec 2010 16:10:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2010/12/12/136195.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/136195.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2010/12/12/136195.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/136195.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/136195.html</trackback:ping><description><![CDATA[<!--TOOLBAR_START--><!--TOOLBAR_EXEMPT--><!--TOOLBAR_END-->
<table border="0" cellspacing="0" cellpadding="0" width="99%">
<tbody>
<tr valign="top">
<td width="280">
<h1>C++ Templates Tutorial</h1></td>
<td align="right"><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/default.htm" target="_top">C++ 
Library</a>&nbsp;&nbsp;<br></td></tr></tbody></table>&nbsp; <br>
<hr>

<h2><em>Table of Contents</em></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T1" target="MAIN">Introduction</a></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T2" target="MAIN">Class Templates</a></h2>
<ul>
<li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T3" target="MAIN">Implementing a class template</a></strong> 
</li><li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T4" target="MAIN">Implementing class template members</a></strong> 
</li><li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T5" target="MAIN">Using a class template</a></strong> </li></ul>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T6" target="MAIN">Function Templates</a></h2>
<ul>
<li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T7" target="MAIN">Implementing Template Functions</a></strong> 
</li><li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T8" target="MAIN">Using Template Functions</a></strong> </li></ul>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T9" target="MAIN">Template Instantiation</a></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T10">Class Template Specialization</a></h2>
<ul>
<li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T11" target="MAIN">Template Class Partial Specialization</a></strong> 
</li></ul>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T12" target="MAIN">Template Function Specialization</a></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T13&quot;" target="MAIN">Template Parameters</a></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T14" target="MAIN">Static Members and Variables</a></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T15" target="MAIN">Templates and Friends</a></h2>
<hr>

<h1><a name="T1">Introduction</a></h1>
<p>Many C++ programs use common data structures like stacks, queues and lists. A 
program may require a queue of customers and a queue of messages. One could 
easily implement a queue of customers, then take the existing code and implement 
a queue of messages. The program grows, and now there is a need for a queue of 
orders. So just take the queue of messages and convert that to a queue of orders 
(Copy, paste, find, replace????). Need to make some changes to the queue 
implementation? Not a very easy task, since the code has been duplicated in many 
places. Re-inventing source code is not an intelligent approach in an object 
oriented environment which encourages re-usability. It seems to make more sense 
to implement a queue that can contain any arbitrary type rather than duplicating 
code. How does one do that? The answer is to use type parameterization, more 
commonly referred to as templates. 
</p><p>C++ templates allow one to implement a generic Queue&lt;T&gt; template that 
has a type parameter T. T can be replaced with actual types, for example, 
Queue&lt;Customers&gt;, and C++ will generate the class Queue&lt;Customers&gt;. 
Changing the implementation of the Queue becomes relatively simple. Once the 
changes are implemented in the template Queue&lt;T&gt;, they are immediately 
reflected in the classes Queue&lt;Customers&gt;, Queue&lt;Messages&gt;, and 
Queue&lt;Orders&gt;. 
</p><p>Templates are very useful when implementing generic constructs like vectors, 
stacks, lists, queues which can be used with any arbitrary type. C++ templates 
provide a way to re-use source code as opposed to inheritance and composition 
which provide a way to re-use object code. 
</p><p>C++ provides two kinds of templates: class templates and function templates. 
Use function templates to write generic functions that can be used with 
arbitrary types. For example, one can write searching and sorting routines which 
can be used with any arbitrary type. The Standard Template Library generic 
algorithms have been implemented as function templates, and the containers have 
been implemented as class templates. 
</p><h1><a name="T2">Class Templates</a></h1>
<h2><a name="T3">Implementing a class template</a></h2>
<p>A class template definition looks like a regular class definition, except it 
is prefixed by the keyword template. For example, here is the definition of a 
class template for a Stack. </p><pre>template &lt;class T&gt;
class Stack
{
public:
	Stack(int = 10) ; 
	~Stack() { delete [] stackPtr ; }
	int push(const T&amp;); 
	int pop(T&amp;) ;  
	int isEmpty()const { return top == -1 ; } 
	int isFull() const { return top == size - 1 ; } 
private:
	int size ;  // number of elements on Stack.
	int top ;  
	T* stackPtr ;  
} ;
</pre>
<p>T is a type parameter and it can be any type. For example, 
Stack&lt;Token&gt;, where Token is a user defined class. T does not have to be a 
class type as implied by the keyword class. For example, Stack&lt;int&gt; and 
Stack&lt;Message*&gt; are valid instantiations, even though int and Message* are 
not "classes". 
</p><h2><a name="T4">Implementing class template member functions</a></h2>
<p>Implementing template member functions is somewhat different compared to the 
regular class member functions. The declarations and definitions of the class 
template member functions should all be in the same header file. The 
declarations and definitions need to be in the same header file. Consider the 
following. <br><br>
<table border="1" cellpadding="5" cols="3">
<tbody>
<tr>
<td><pre>//B.H
template &lt;class t&gt;
class b
{
public:
	b() ;
	~b() ;
} ;
</pre></td>
<td><pre>// B.CPP
#include "B.H"
template &lt;class t&gt;
b&lt;t&gt;::b()
{
}
template &lt;class t&gt;
b&lt;t&gt;::~b()
{
}
</pre></td>
<td><pre>//MAIN.CPP
#include "B.H"
void main()
{
	 b&lt;int&gt; bi ;
	 b &lt;float&gt; bf ;
}
</pre></td></tr></tbody></table>
</p><p>When compiling B.cpp, the compiler has both the declarations and the 
definitions available. At this point the compiler does not need to generate any 
definitions for template classes, since there are no instantiations. When the 
compiler compiles main.cpp, there are two instantiations: template class 
B&lt;int&gt; and B&lt;float&gt;. At this point the compiler has the declarations 
but no definitions! 
</p><p>While implementing class template member functions, the definitions are 
prefixed by the keyword template. Here is the complete implementation of class 
template Stack: </p><pre>//stack.h
#pragma once
template &lt;class T&gt;
class Stack
{
public:
	Stack(int = 10) ; 
	~Stack() { delete [] stackPtr ; }
	int push(const T&amp;); 
	int pop(T&amp;) ;  // pop an element off the stack
	int isEmpty()const { return top == -1 ; } 
	int isFull() const { return top == size - 1 ; } 
private:
	int size ;  // Number of elements on Stack
	int top ;  
	T* stackPtr ;  
} ;

//constructor with the default size 10
template &lt;class T&gt;
Stack&lt;T&gt;::Stack(int s)
{
	size = s &gt; 0 &amp;&amp; s &lt; 1000 ? s : 10 ;  
	top = -1 ;  // initialize stack
	stackPtr = new T[size] ; 
}
 // push an element onto the Stack 
template &lt;class T&gt;
int Stack&lt;T&gt;::push(const T&amp; item)
{
	if (!isFull())
	{
		stackPtr[++top] = item ;
		return 1 ;  // push successful
	}
	return 0 ;  // push unsuccessful
}

// pop an element off the Stack
template &lt;class T&gt; 
int Stack&lt;T&gt;::pop(T&amp; popValue) 
{
	if (!isEmpty())
	{
		popValue = stackPtr[top--] ;
		return 1 ;  // pop successful
	}
	return 0 ;  // pop unsuccessful
}
</pre>
<h2><a name="T5">Using a class template</a></h2>
<p>Using a class template is easy. Create the required classes by plugging in 
the actual type for the type parameters. This process is commonly known as 
"Instantiating a class". Here is a sample driver class that uses the Stack class 
template. </p><pre>#include &lt;iostream&gt;
#include "stack.h"
using namespace std ;
void main()
{
	typedef Stack&lt;float&gt; FloatStack ;
	typedef Stack&lt;int&gt; IntStack ;

	FloatStack fs(5) ;
	float f = 1.1 ;
	cout &lt;&lt; "Pushing elements onto fs" &lt;&lt; endl ;
	while (fs.push(f))
	{
		cout &lt;&lt; f &lt;&lt; ' ' ;
		f += 1.1 ;
	}
	cout &lt;&lt; endl &lt;&lt; "Stack Full." &lt;&lt; endl
	&lt;&lt; endl &lt;&lt; "Popping elements from fs" &lt;&lt; endl ;
	while (fs.pop(f))
		cout &lt;&lt; f &lt;&lt; ' ' ;
	cout &lt;&lt; endl &lt;&lt; "Stack Empty" &lt;&lt; endl ;
	cout &lt;&lt; endl ;

	IntStack is ;
	int i = 1.1 ;
	cout &lt;&lt; "Pushing elements onto is" &lt;&lt; endl ;
	while (is.push(i))
	{
		cout &lt;&lt; i &lt;&lt; ' ' ;
		i += 1 ;
	}
	cout &lt;&lt; endl &lt;&lt; "Stack Full" &lt;&lt; endl
	&lt;&lt; endl &lt;&lt; "Popping elements from is" &lt;&lt; endl ;
	while (is.pop(i))
			cout &lt;&lt; i &lt;&lt; ' ' ;
	cout &lt;&lt; endl &lt;&lt; "Stack Empty" &lt;&lt; endl ;
}
</pre>
<h3>Program Output</h3><pre>Pushing elements onto fs
1.1 2.2 3.3 4.4 5.5 
Stack Full.

Popping elements from fs
5.5 4.4 3.3 2.2 1.1 
Stack Empty

Pushing elements onto is
1 2 3 4 5 6 7 8 9 10 
Stack Full

Popping elements from is
10 9 8 7 6 5 4 3 2 1 
Stack Empty
</pre>
<p>In the above example we defined a class template Stack. In the driver program 
we instantiated a Stack of float (FloatStack) and a Stack of int(IntStack). Once 
the template classes are instantiated you can instantiate objects of that type 
(for example, fs and is.) 
</p><p>A good programming practice is using typedef while instantiating template 
classes. Then throughout the program, one can use the typedef name. There are 
two advantages: 
</p><ul>
<li><strong>typedef's</strong> are very useful when "templates of templates" come into 
usage. For example, when instantiating an STL vector of int's, you could use: <pre>		 typedef vector&lt;int, allocator&lt;int&gt; &gt; INTVECTOR ; 
	</pre>
</li><li>If the template definition changes, simply change the typedef definition. 
For example, currently the definition of template class vector requires a second 
parameter. <pre>		typedef vector&lt;int, allocator&lt;int&gt; &gt; INTVECTOR ;
		INTVECTOR vi1 ;
	</pre>In a future version, the second parameter may not be required, for 
example, <pre>		typedef vector&lt;int&gt; INTVECTOR ;
		INTVECTOR vi1 ;
	</pre></li></ul>
<p>Imagine how many changes would be required if there was no <strong>typedef</strong>! 
</p><h1><a name="T6">Function Templates</a></h1>
<p>To perform identical operations for each type of data compactly and 
conveniently, use function templates. You can write a single function template 
definition. Based on the argument types provided in calls to the function, the 
compiler automatically instantiates separate object code functions to handle 
each type of call appropriately. The STL algorithms are implemented as function 
templates. 
</p><h2><a name="T7">Implementing Template Functions</a></h2>
<p>Function templates are implemented like regular functions, except they are 
prefixed with the keyword template. Here is a sample with a function template. </p><pre>#include &lt;iostream&gt;
using namespace std ;
//max returns the maximum of the two elements
template &lt;class T&gt;
T max(T a, T b)
{
	return a &gt; b ? a : b ;
}
</pre>
<h2><a name="T8">Using Template Functions</a></h2>
<p>Using function templates is very easy: just use them like regular functions. 
When the compiler sees an instantiation of the function template, for example: 
the call max(10, 15) in function main, the compiler generates a function 
max(int, int). Similarly the compiler generates definitions for max(char, char) 
and max(float, float) in this case. </p><pre>#include &lt;iostream&gt;
using namespace std ;
//max returns the maximum of the two elements
template &lt;class T&gt;
T max(T a, T b)
{
	return a &gt; b ? a : b ;
}
void main()
{
   
	cout &lt;&lt; "max(10, 15) = " &lt;&lt; max(10, 15) &lt;&lt; endl ;
	cout &lt;&lt; "max('k', 's') = " &lt;&lt; max('k', 's') &lt;&lt; endl ;
	cout &lt;&lt; "max(10.1, 15.2) = " &lt;&lt; max(10.1, 15.2) &lt;&lt; endl ;
}
</pre>
<h3>Program Output</h3><pre>max(10, 15) = 15
max('k', 's') = s
max(10.1, 15.2) = 15.2
</pre>
<h1><a name="T9">Template Instantiation</a></h1>
<p>When the compiler generates a class, function or static data members from a 
template, it is referred to as template instantiation. 
</p><ul>
<li>A class generated from a class template is called a generated class. 
</li><li>A function generated from a function template is called a generated 
function. 
</li><li>A static data member generated from a static data member template is called 
a generated static data member. </li></ul>
<p>The compiler generates a class, function or static data members from a 
template when it sees an implicit instantiation or an explicit instantiation of 
the template. 
</p><ol>
<li>Consider the following sample. This is an example of implicit instantiation 
of a class template. <pre>template &lt;class T&gt;
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
Z&lt;int&gt; zi ;   //implicit instantiation generates class Z&lt;int&gt;
Z&lt;float&gt; zf ; //implicit instantiation generates class Z&lt;float&gt;
return 0 ;
}
</pre>
</li><li>Consider the following sample. This sample uses the template class members 
Z&lt;T&gt;::f() and Z&lt;T&gt;::g(). <pre>template &lt;class T&gt;
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
Z&lt;int&gt; zi ; //implicit instantiation generates class Z&lt;int&gt;
zi.f() ;    //and generates function Z&lt;int&gt;::f()
Z&lt;float&gt; zf ; //implicit instantiation generates class Z&lt;float&gt;
zf.g() ;      //and generates function Z&lt;float&gt;::g()
return 0 ;
}
</pre>
<p>This time in addition to the generating classes Z&lt;int&gt; and 
Z&lt;float&gt;, with constructors and destructors, the compiler also generates 
definitions for Z&lt;int&gt;::f() and Z&lt;float&gt;::g(). The compiler does not 
generate definitions for functions, nonvirtual member functions, class or member 
class that does not require instantiation. In this example, the compiler did not 
generate any definitions for Z&lt;int&gt;::g() and Z&lt;float&gt;::f(), since 
they were not required. 
</p><p>&#160;</p>
</li><li>Consider the following sample. This is an example of explicit instantiation 
of a class template. <pre>template &lt;class T&gt;
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
template class Z&lt;int&gt; ; //explicit instantiation of class Z&lt;int&gt;
template class Z&lt;float&gt; ; //explicit instantiation of 
                          //class Z&lt;float&gt;
return 0 ;
}
</pre>
</li><li>Consider the following sample. Will the compiler generate any classes in 
this case? The answer is NO. <pre>template &lt;class T&gt;
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
Z&lt;int&gt;* p_zi ; //instantiation of class Z&lt;int&gt; not required
Z&lt;float&gt;* p_zf ; //instantiation of class Z&lt;float&gt; not required
return 0 ;
}
</pre>
<p>This time the compiler does not generate any definitions! There is no need 
for any definitions. It is similar to declaring a pointer to an undefined class 
or struct. 
</p><p>&#160;</p>
</li><li>Consider the following sample. This is an example of implicit instantiation 
of a function template. <pre>//max returns the maximum of the two elements
template &lt;class T&gt;
T max(T a, T b)
{
    return a &gt; b ? a : b ;
}
void main()
{
int I ;
I = max(10, 15) ; //implicit instantiation of max(int, int)
char c ;
c = max('k', 's') ; //implicit instantiation of max(char, char)
}
</pre>
<p>In this case the compiler generates functions max(int, int) and max(char, 
char). The compiler generates definitions using the template function max. 
</p><p>&#160;</p>
</li><li>Consider the following sample. This is an example of explicit instantiation 
of a function template. <pre>template &lt;class T&gt;
void Test(T r_t)
{
}

int main()
{
//explicit instantiation of Test(int)
	template void Test&lt;int&gt;(int) ;
	return 0 ;
}
</pre><strong>NOTE</strong>: Visual C++ 5.0 does not support this syntax currently. The 
above sample causes compiler error C1001. 
<p>In this case the compiler would generate function Test(int). The compiler 
generates the definition using the template function Test. 
</p><p>&#160;</p>
</li><li>If an instantiation of a class template is required, and the template 
declared but not defined, the program is ill-formed. VC5.0 compiler generates 
error C2079. <pre>template &lt;class T&gt; class X ;

int main()
{
	X&lt;int&gt; xi ; //error C2079: 'xi' uses undefined class 'X&lt;int&gt;'
	return 0 ;
}
</pre>
</li><li>Instantiating virtual member functions of a class template that does not 
require instantiation is implementation defined. For example, in the following 
sample, virtual function X&lt;T&gt;::Test() is not required, VC5.0 generates a 
definition for X&lt;T&gt;::Test. <pre>template &lt;class T&gt; 
class X 
{
	public:
		virtual void Test() {} 
};

int main()
{
	X&lt;int&gt; xi ; //implicit instantiation of X&lt;int&gt;
	return 0 ;
}
</pre>
<p>In this case the compiler generates a definition for X&lt;int&gt;::Test, even 
if it is not required. </p></li></ol>
<h1><a name="T10">Class Template Specialization</a></h1>
<p>In some cases it is possible to override the template-generated code by 
providing special definitions for specific types. This is called template 
specialization. The following example defines a template class specialization 
for template class stream. </p><pre>#include &lt;iostream&gt;
using namespace std ;

template &lt;class T&gt;
class stream
{
	public:
		void f() { cout &lt;&lt; "stream&lt;T&gt;::f()"&lt;&lt; endl ;}
} ;

template &lt;&gt;
class stream&lt;char&gt;
{
	public:
		void f() { cout &lt;&lt; "stream&lt;char&gt;::f()"&lt;&lt; endl ;}
} ;

int main()
{
	stream&lt;int&gt; si ;
	stream&lt;char&gt; sc ;

	si.f() ;
	sc.f() ;
	
	return 0 ;
}
</pre>
<h3>Program Output</h3><pre>stream&lt;T&gt;::f()
stream&lt;char&gt;::f()
</pre>
<p>In the above example, stream&lt;char&gt; is used as the definition of streams 
of chars; other streams will be handled by the template class generated from the 
class template. 
</p><h2><a name="T11">Template Class Partial Specialization</a></h2>
<p>You may want to generate a specialization of the class for just one 
parameter, for example </p><pre>//base template class
template&lt;typename T1, typename T2&gt; 
class X 
{
} ;

//partial specialization
template&lt;typename T1&gt; 
class X&lt;T1, int&gt; 
{
} ; //C2989 here

int main()
{
	// generates an instantiation from the base template
	X&lt;char, char&gt; xcc ; 
	
	//generates an instantiation from the partial specialization
X&lt;char, int&gt; xii ;  

	return 0 ;
}
</pre>
<p>A partial specialization matches a given actual template argument list if the 
template arguments of the partial specialization can be deduced from the actual 
template argument list. 
</p><p><strong>NOTE</strong>: Visual C++ 5.0 does not support template class partial 
specialization. The above sample causes compiler error C2989: template class has 
already been defined as a non-template class. 
</p><h1><a name="T12">Template Function Specialization</a></h1>
<p>In some cases it is possible to override the template-generated code by 
providing special definitions for specific types. This is called template 
specialization. The following example demonstrates a situation where overriding 
the template generated code would be necessary: </p><pre>#include &lt;iostream&gt;
using namespace std ;

//max returns the maximum of the two elements of type T, where T is a
//class or data type for which operator&gt; is defined.
template &lt;class T&gt;
T max(T a, T b)
{
    return a &gt; b ? a : b ;
}

int main()
{    
    cout &lt;&lt; "max(10, 15) = " &lt;&lt; max(10, 15) &lt;&lt; endl ;
    cout &lt;&lt; "max('k', 's') = " &lt;&lt; max('k', 's') &lt;&lt; endl ;
    cout &lt;&lt; "max(10.1, 15.2) = " &lt;&lt; max(10.1, 15.2) &lt;&lt; endl ;
    cout &lt;&lt; "max(\"Aladdin\", \"Jasmine\") = " &lt;&lt; max("Aladdin", "Jasmine") &lt;&lt; endl ;
    return 0 ;
}
</pre>
<h3>Program Output</h3><pre>max(10, 15) = 15
max('k', 's') = s
max(10.1, 15.2) = 15.2
max("Aladdin", "Jasmine") = Aladdin
</pre>
<p>Not quite the expected results! Why did that happen? The function call 
max("Aladdin", "Jasmine") causes the compiler to generate code for max(char*, 
char*), which compares the addresses of the strings! To correct special cases 
like these or to provide more efficient implementations for certain types, one 
can use template specializations. The above example can be rewritten with 
specialization as follows: </p><pre>#include &lt;iostream&gt;
#include &lt;cstring&gt;
using namespace std ;

//max returns the maximum of the two elements
template &lt;class T&gt;
T max(T a, T b)
{
    return a &gt; b ? a : b ;
}

// Specialization of max for char*
template &lt;&gt;
char* max(char* a, char* b)
{
    return strcmp(a, b) &gt; 0 ? a : b ;
}

int main()
{
    
    cout &lt;&lt; "max(10, 15) = " &lt;&lt; max(10, 15) &lt;&lt; endl ;
    cout &lt;&lt; "max('k', 's') = " &lt;&lt; max('k', 's') &lt;&lt; endl ;
    cout &lt;&lt; "max(10.1, 15.2) = " &lt;&lt; max(10.1, 15.2) &lt;&lt; endl ;
    cout &lt;&lt; "max(\"Aladdin\", \"Jasmine\") = " &lt;&lt; max("Aladdin", "Jasmine") &lt;&lt; endl ;
    return 0 ;
}
</pre>
<h3>Program Output</h3><pre>max(10, 15) = 15
max('k', 's') = s
max(10.1, 15.2) = 15.2
max("Aladdin", "Jasmine") = Jasmine
</pre>
<h1><a name="T13">Template Parameters</a></h1>
<ol>
<li>C++ templates allow one to implement a generic Queue&lt;T&gt; template that 
has a type parameter T. T can be replaced with actual types, for example, 
Queue&lt;Customers&gt;, and C++ will generate the class Queue&lt;Customers&gt;. 
For example, <pre>template &lt;class T&gt;
class Stack
{
} ;
</pre>
<p>Here T is a template parameter, also referred to as type-parameter. 
</p><p>&#160;</p>
</li><li>C++ allows you to specify a default template parameter, so the definition 
could now look like: <pre>	template &lt;class T = float, int elements = 100&gt; Stack { ....} ;
</pre>
<p>Then a declaration such as </p><pre>	Stack&lt;&gt; mostRecentSalesFigures ;	
</pre>
<p>would instantiate (at compile time) a 100 element Stack template class named 
mostRecentSalesFigures of float values; this template class would be of type 
Stack&lt;float, 100&gt;. 
</p><p>Note, C++ also allows non-type template parameters. In this case, template 
class Stack has an int as a non-type parameter. 
</p><p><strong>If you specify a default template parameter for any formal parameter, the 
rules are the same as for functions and default parameters. Once a default 
parameter is declared all subsequent parameters must have defaults.</strong> 
</p><p>&#160;</p>
</li><li>Default arguments cannot be specified in a declaration or a definition of a 
specialization. For example, <pre>template &lt;class T, int size&gt;
class Stack
{
} ;

//error C2989: 'Stack&lt;int,10&gt;' : template class has already been
//defined as a non-template class
template &lt;class T, int size = 10&gt;
class Stack&lt;int, 10&gt;
{
} ;

int main()
{
	Stack&lt;float,10&gt; si ;
              return 0 ;
}
</pre>
<p>&#160;</p>
</li><li>A type-parameter defines its identifier to be a type-name in the scope of 
the template declaration, and canot be re-declared within its scope (including 
nested scopes). For example, <pre>template &lt;class T, int size&gt;
class Stack
{
	int T ; //error type-parameter re-defined.
	void f()
	{
		char T ; //error type-parameter re-defined.
	}
} ;

class A {} ;
int main()
{
	Stack&lt;A,10&gt; si ;
       return 0 ;
}
</pre>
<p><strong>NOTE</strong>: VC++ 5.0 or SP1 compiles this sample without any errors. It does 
not flag the re-definition of type-parameter as an error. 
</p><p>&#160;</p>
</li><li>The value of a non-type-parameter cannot be assigned to or have its value 
changed. For example, <pre>template &lt;class T, int size&gt;
class Stack
{
	void f()
	{
		//error C2105: '++' needs l-value             
             size++ ; //error change of template argument value
	}
} ;


int main()
{
	Stack&lt;double,10&gt; si ;
       return 0 ;
}
</pre>
</li><li>A template-parameter that could be interpreted as either a 
parameter-declaration or a type-parameter, is taken as a type-parameter. For 
example, <pre>class T {} ;
int i ;

template &lt;class T, T i&gt;
void f(T t)
{
	T t1 = i ; //template arguments T and i
	::T t2 = ::i ; //globals T and i 
} 



int main()
{
	f('s') ; //C2783 here
	return 0 ;
}
</pre>
<p><strong>NOTE</strong>: Compiling the above sample using VC++ 5.0 and SP1 causes 
compiler error C2783: could not deduce template argument for 'i'. To workaround 
the problem, replace the call to f('s') with f&lt;char, 's'&gt;('s'). </p><pre>class T {} ;
int i ;

template &lt;class T, T i&gt;
void f(T t)
{
	T t1 = i ; //template arguments T and i
	::T t2 = ::i ; //globals T and i 
} 

int main()
{
	f&lt;char, 's'&gt;('s') ; //workaround
	return 0 ;
}
</pre>
</li><li>A non-type template parameter cannot be of floating type. For example, <pre>template &lt;double d&gt; class X ; //error C2079: 'xd' uses 
                              //undefined class 'X&lt;1.e66&gt;'
//template &lt;double* pd&gt; class X ; //ok
//template &lt;double&amp; rd&gt; class X ; //ok

int main()
{
	X&lt;1.0&gt; xd ;
	return 0 ;
}
</pre></li></ol>
<h1><a name="T14">Static Members and Variables</a></h1>
<ol>
<li>Each template class or function generated from a template has its own copies 
of any static variables or members. 
<p>&#160;</p>
</li><li>Each instantiation of a function template has it's own copy of any static 
variables defined within the scope of the function. For example, <pre>template &lt;class T&gt;
class X
{
    public:
	static T s ;
} ;

int main()
{
	X&lt;int&gt; xi ;
       X&lt;char*&gt; xc ;
}
</pre>
<p>Here X&lt;int&gt; has a static data member s of type int and X&lt;char*&gt; 
has a static data member s of type char*. 
</p><p>&#160;</p>
</li><li>Static members are defined as follows. <pre>#include &lt;iostream&gt;
using namespace std ;

template &lt;class T&gt;
class X
{
    public:
	static T s ;
} ;

template &lt;class T&gt; T X&lt;T&gt;::s = 0 ;
template &lt;&gt; int X&lt;int&gt;::s = 3 ;
template &lt;&gt; char* X&lt;char*&gt;::s = "Hello" ;

int main()
{
	X&lt;int&gt; xi ;
	cout &lt;&lt; "xi.s = " &lt;&lt; xi.s &lt;&lt; endl ;

       X&lt;char*&gt; xc ;
	cout &lt;&lt; "xc.s = " &lt;&lt; xc.s &lt;&lt; endl ;
	
	return 0 ;
}
</pre>
<h3>Program Output</h3><pre>xi.s = 10
xc.s = Hello
</pre>
</li><li>Each instantiation of a function template has it's own copy of the static 
variable. For example, <pre>#include &lt;iostream&gt;
using namespace std ;

template &lt;class T&gt;
void f(T t)
{
	static T s  = 0;
	s = t ;
	cout &lt;&lt; "s = " &lt;&lt; s &lt;&lt; endl ;
} 

int main()
{
	f(10) ;
	f("Hello") ;
	
	return 0 ;
}
</pre>
<h3>Program Output</h3><pre>s = 10
s = Hello
</pre>
<p>Here f&lt;int&gt;(int) has a static variable s of type int, and 
f&lt;char*&gt;(char*) has a static variable s of type char*. </p></li></ol>
<h1><a name="T15">Templates and Friends</a></h1>
<p>Friendship can be established between a class template and a global function, 
a member function of another class (possibly a template class), or even an 
entire class (possible template class). The table below lists the results of 
declaring different kinds of friends of a class. 
</p><p>
<table border="1" cellpadding="5" cols="3">
<tbody>
<tr>
<td align="middle"><strong>Class Template</strong> </td>
<td align="middle"><strong>friend declaration in class template X</strong> </td>
<td align="middle"><strong>Results of giving friendship</strong> </td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend void f1() ; </td>
<td>makes f1() a friend of all instantiations of template X. For example, f1() 
is a friend of X&lt;int&gt;, X&lt;A&gt;, and X&lt;Y&gt;. </td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend void f2(X&lt;T&gt;&amp;) ; </td>
<td>For a particular type T for example, float, makes f2(X&lt;float&gt;&amp;) a 
friend of class X&lt;float&gt; only. f2(x&lt;float&gt;&amp;) cannot be a friend 
of class X&lt;A&gt;. </td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend A::f4() ; // A is a user defined class with a member function f4() ; 
</td>
<td>makes A::f4() a friend of all instantiations of template X. For example, 
A::f4() is a friend of X&lt;int&gt;, X&lt;A&gt;, and X&lt;Y&gt;. </td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend C&lt;T&gt;::f5(X&lt;T&gt;&amp;) ; // C is a class template with a 
member function f5 </td>
<td>For a particular type T for example, float, makes 
C&lt;float&gt;::f5(X&lt;float&gt;&amp;) a friend of class X&lt;float&gt; only. 
C&lt;float&gt;::f5(x&lt;float&gt;&amp;) cannot be a friend of class X&lt;A&gt;. 
</td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend class Y ; </td>
<td>makes every member function of class Y a friend of every template class 
produced from the class template X. </td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend class Z&lt;T&gt; ; </td>
<td>when a template class is instantiated with a particular type T, such as a 
float, all members of class Z&lt;float&gt; become friends of template class 
X&lt;float&gt;. </td></tr></tbody></table>
</p><hr>
&#169; 1997 Microsoft Corporation. All rights reserved. Terms of Use.
<img src ="http://www.cppblog.com/koople/aggbug/136195.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2010-12-12 00:10 <a href="http://www.cppblog.com/koople/archive/2010/12/12/136195.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>char *  和 memcpy</title><link>http://www.cppblog.com/koople/archive/2010/11/27/134858.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Sat, 27 Nov 2010 15:44:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2010/11/27/134858.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/134858.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2010/11/27/134858.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/134858.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/134858.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 最近项目里面经常使用到memcpy,使用memcpy的时候，如下代码
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_48_150_Open_Image onclick="this.style.display='none'; Codehighlighter1_48_150_Open_Text.style.display='none'; Codehighlighter1_48_150_Closed_Image.style.display='inline'; Codehighlighter1_48_150_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_48_150_Closed_Image onclick="this.style.display='none'; Codehighlighter1_48_150_Closed_Text.style.display='none'; Codehighlighter1_48_150_Open_Image.style.display='inline'; Codehighlighter1_48_150_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_48_150_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_48_150_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World&nbsp;Again!</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">memcpy(dst,src,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(src));<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
报错说内存地址错误..也就是Unhandled exception in&nbsp;.exe：0xC0000005:Access Violation。<br><br>&nbsp;&nbsp;&nbsp;&nbsp; 没事就研究了下memcpy&nbsp;，memcpy在VC6.0版本中的代码如下
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;__cdecl&nbsp;memcpy&nbsp;(<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst,<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src,<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;count<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img id=Codehighlighter1_101_744_Open_Image onclick="this.style.display='none'; Codehighlighter1_101_744_Open_Text.style.display='none'; Codehighlighter1_101_744_Closed_Image.style.display='inline'; Codehighlighter1_101_744_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_101_744_Closed_Image onclick="this.style.display='none'; Codehighlighter1_101_744_Closed_Text.style.display='none'; Codehighlighter1_101_744_Open_Image.style.display='inline'; Codehighlighter1_101_744_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_101_744_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_101_744_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;dst;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">#if</span><span style="COLOR: #000000">&nbsp;defined&nbsp;(_M_MRX000)&nbsp;||&nbsp;defined&nbsp;(_M_ALPHA)&nbsp;||&nbsp;defined&nbsp;(_M_PPC)</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img id=Codehighlighter1_204_330_Open_Image onclick="this.style.display='none'; Codehighlighter1_204_330_Open_Text.style.display='none'; Codehighlighter1_204_330_Closed_Image.style.display='inline'; Codehighlighter1_204_330_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_204_330_Closed_Image onclick="this.style.display='none'; Codehighlighter1_204_330_Closed_Text.style.display='none'; Codehighlighter1_204_330_Open_Image.style.display='inline'; Codehighlighter1_204_330_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_204_330_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_204_330_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;RtlMoveMemory(&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">,&nbsp;size_t&nbsp;count&nbsp;);<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RtlMoveMemory(&nbsp;dst,&nbsp;src,&nbsp;count&nbsp;);<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">#else</span><span style="COLOR: #000000">&nbsp;&nbsp;/*&nbsp;defined&nbsp;(_M_MRX000)&nbsp;||&nbsp;defined&nbsp;(_M_ALPHA)&nbsp;||&nbsp;defined&nbsp;(_M_PPC)&nbsp;*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img id=Codehighlighter1_415_485_Open_Image onclick="this.style.display='none'; Codehighlighter1_415_485_Open_Text.style.display='none'; Codehighlighter1_415_485_Closed_Image.style.display='inline'; Codehighlighter1_415_485_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_415_485_Closed_Image onclick="this.style.display='none'; Codehighlighter1_415_485_Closed_Text.style.display='none'; Codehighlighter1_415_485_Open_Image.style.display='inline'; Codehighlighter1_415_485_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_415_485_Closed_Text>/**/</span><span id=Codehighlighter1_415_485_Open_Text><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;copy&nbsp;from&nbsp;lower&nbsp;addresses&nbsp;to&nbsp;higher&nbsp;addresses<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_511_644_Open_Image onclick="this.style.display='none'; Codehighlighter1_511_644_Open_Text.style.display='none'; Codehighlighter1_511_644_Closed_Image.style.display='inline'; Codehighlighter1_511_644_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_511_644_Closed_Image onclick="this.style.display='none'; Codehighlighter1_511_644_Closed_Text.style.display='none'; Codehighlighter1_511_644_Open_Image.style.display='inline'; Codehighlighter1_511_644_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(count</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_511_644_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_511_644_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">#endif</span><span style="COLOR: #000000">&nbsp;&nbsp;/*&nbsp;defined&nbsp;(_M_MRX000)&nbsp;||&nbsp;defined&nbsp;(_M_ALPHA)&nbsp;||&nbsp;defined&nbsp;(_M_PPC)&nbsp;*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">(ret);<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
实际上 memcpy 使用的代码是从第19行开始的<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;__cdecl&nbsp;memcpy&nbsp;(<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;count<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br><img id=Codehighlighter1_101_309_Open_Image onclick="this.style.display='none'; Codehighlighter1_101_309_Open_Text.style.display='none'; Codehighlighter1_101_309_Closed_Image.style.display='inline'; Codehighlighter1_101_309_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_101_309_Closed_Image onclick="this.style.display='none'; Codehighlighter1_101_309_Closed_Text.style.display='none'; Codehighlighter1_101_309_Open_Image.style.display='inline'; Codehighlighter1_101_309_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_101_309_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_101_309_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;dst;<br><img id=Codehighlighter1_153_286_Open_Image onclick="this.style.display='none'; Codehighlighter1_153_286_Open_Text.style.display='none'; Codehighlighter1_153_286_Closed_Image.style.display='inline'; Codehighlighter1_153_286_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_153_286_Closed_Image onclick="this.style.display='none'; Codehighlighter1_153_286_Closed_Text.style.display='none'; Codehighlighter1_153_286_Open_Image.style.display='inline'; Codehighlighter1_153_286_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(count</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_153_286_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_153_286_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">(ret);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
为了可以进入 中进行调试<br>我使用以下代码调试到memcpy中&nbsp;<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;__cdecl&nbsp;memcpy&nbsp;(<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;count<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br><img id=Codehighlighter1_117_323_Open_Image onclick="this.style.display='none'; Codehighlighter1_117_323_Open_Text.style.display='none'; Codehighlighter1_117_323_Closed_Image.style.display='inline'; Codehighlighter1_117_323_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_117_323_Closed_Image onclick="this.style.display='none'; Codehighlighter1_117_323_Closed_Text.style.display='none'; Codehighlighter1_117_323_Open_Image.style.display='inline'; Codehighlighter1_117_323_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_117_323_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_117_323_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;dst;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img id=Codehighlighter1_169_300_Open_Image onclick="this.style.display='none'; Codehighlighter1_169_300_Open_Text.style.display='none'; Codehighlighter1_169_300_Closed_Image.style.display='inline'; Codehighlighter1_169_300_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_169_300_Closed_Image onclick="this.style.display='none'; Codehighlighter1_169_300_Closed_Text.style.display='none'; Codehighlighter1_169_300_Open_Image.style.display='inline'; Codehighlighter1_169_300_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(count</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_169_300_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_169_300_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">(ret);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_339_441_Open_Image onclick="this.style.display='none'; Codehighlighter1_339_441_Open_Text.style.display='none'; Codehighlighter1_339_441_Closed_Image.style.display='inline'; Codehighlighter1_339_441_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_339_441_Closed_Image onclick="this.style.display='none'; Codehighlighter1_339_441_Closed_Text.style.display='none'; Codehighlighter1_339_441_Open_Image.style.display='inline'; Codehighlighter1_339_441_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_339_441_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_339_441_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World&nbsp;Again!</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">memcpy(dst,src,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(src));<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
最后发现错误出在memcpy函数中
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src;</span></div>
为什么呢？<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World&nbsp;Again!</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;</span></div>
这里dst是在文字常量区分配一个字符串常量，再把str1指向它<br>等于就是<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World&nbsp;Again!</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;</span></div>
所以这里已给字符指针所指向的字符变量赋值就出错。<br>如果要想使以上代码顺利运行 可以将main中的代码改为如下代码<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_12_106_Open_Image onclick="this.style.display='none'; Codehighlighter1_12_106_Open_Text.style.display='none'; Codehighlighter1_12_106_Closed_Image.style.display='inline'; Codehighlighter1_12_106_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_12_106_Closed_Image onclick="this.style.display='none'; Codehighlighter1_12_106_Closed_Text.style.display='none'; Codehighlighter1_12_106_Open_Image.style.display='inline'; Codehighlighter1_12_106_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_12_106_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_12_106_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">memcpy(dst,src,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(src));<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
或者<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_12_94_Open_Image onclick="this.style.display='none'; Codehighlighter1_12_94_Open_Text.style.display='none'; Codehighlighter1_12_94_Closed_Image.style.display='inline'; Codehighlighter1_12_94_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_12_94_Closed_Image onclick="this.style.display='none'; Codehighlighter1_12_94_Closed_Text.style.display='none'; Codehighlighter1_12_94_Open_Image.style.display='inline'; Codehighlighter1_12_94_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_12_94_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_12_94_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;dst[</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">memcpy(dst,src,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(src));<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<img src ="http://www.cppblog.com/koople/aggbug/134858.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2010-11-27 23:44 <a href="http://www.cppblog.com/koople/archive/2010/11/27/134858.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CDateTimeCtrl' : missing storage-class or type specifiers 编译错误处理</title><link>http://www.cppblog.com/koople/archive/2010/11/21/134240.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Sun, 21 Nov 2010 12:51:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2010/11/21/134240.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/134240.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2010/11/21/134240.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/134240.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/134240.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;加入一个CDateTimeCtrl 控件 进对话框后居然报错CDateTimeCtrl' : missing storage-class or type specifiers<div>http://www.codeguru.com/forum/showthread.php?t=50341</div><div>如下处理</div><div><span  style="font-family: verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; font-size: 13px; "><table class="tborder" id="post131613" cellpadding="3" cellspacing="1" border="0" width="100%" align="center" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(91, 93, 67); color: rgb(0, 0, 0); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(11, 25, 140); border-right-color: rgb(11, 25, 140); border-bottom-color: rgb(11, 25, 140); border-left-color: rgb(11, 25, 140); background-position: initial initial; background-repeat: initial initial; "><tbody><tr><td class="thead" style="font: normal normal bold 11px/normal tahoma, verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(91, 93, 67); color: rgb(255, 255, 255); padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; background-position: initial initial; background-repeat: initial initial; "><div class="normal" style="font-weight: normal; float: right; ">&nbsp; #<a href="http://www.codeguru.com/forum/showpost.php?p=131613&amp;postcount=7" target="new" rel="nofollow" id="postcount131613" name="7" style="color: rgb(255, 255, 255); "><strong>7</strong></a>&nbsp;&nbsp;&nbsp;&nbsp;</div><div class="normal" style="font-weight: normal; "><a name="post131613"><img class="inlineimg" src="http://www.codeguru.com/forum/images/statusicon/post_old.gif" alt="Old" border="0" title="Old" style="vertical-align: middle; "></a>&nbsp;May 28th, 1999, 07:03 PM</div></td></tr><tr><td class="alt2" style="font: normal normal normal 10pt/normal verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); color: rgb(0, 0, 0); padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; background-position: initial initial; background-repeat: initial initial; "><table cellpadding="0" cellspacing="3" border="0" width="100%"><tbody><tr><td nowrap="nowrap" style="font: normal normal normal 10pt/normal verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; "><div id="postmenu_131613"><a class="bigusername" href="http://www.codeguru.com/forum/member.php?u=1304" style="font-size: 14pt; color: rgb(5, 48, 97); ">Tonio</a>&nbsp;<img class="inlineimg" src="http://www.codeguru.com/forum/images/statusicon/user_offline.gif" alt="Tonio is offline" border="0" title="Tonio is offline" style="vertical-align: middle; "></div><div class="smallfont" style="font: normal normal normal 11px/normal verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; ">Member</div></td><td width="100%" style="font: normal normal normal 10pt/normal verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; ">&nbsp;</td><td valign="top" nowrap="nowrap" style="font: normal normal normal 10pt/normal verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; "><div class="smallfont" style="font: normal normal normal 11px/normal verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; "><div>Join Date: Apr 1999</div><div>Location: Philippines</div><div>Posts: 46</div><div><span id="repdisplay_131613_1304"><img class="inlineimg" src="http://www.codeguru.com/forum/images/reputation/reputation_pos.gif" alt="Tonio is an unknown quantity at this point (&lt;10)" border="0" title="Tonio is an unknown quantity at this point (&lt;10)" style="vertical-align: middle; "></span></div><div></div></div></td></tr></tbody></table></td></tr><tr><td class="alt1" id="td_post_131613" style="font: normal normal normal 10pt/normal verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(245, 245, 255); color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial; "><div class="smallfont" style="font: normal normal normal 11px/normal verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif; "><strong>Re: CDateTimeCtrl problem</strong></div><hr size="1" style="color: rgb(91, 93, 67); background-color: rgb(91, 93, 67); "><div class="litcontent"><div id="post_message_131613">Good Day!<br>including the "AFXDTCTL.H" in my stdafx.h file made my day!<br>many thanks to ric,walter i an and ERIC33 for the support!</div></div></td></tr></tbody></table></span></div><img src ="http://www.cppblog.com/koople/aggbug/134240.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2010-11-21 20:51 <a href="http://www.cppblog.com/koople/archive/2010/11/21/134240.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>COM应用原理使用VS2005打开遇到的问题</title><link>http://www.cppblog.com/koople/archive/2010/05/26/116339.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Tue, 25 May 2010 21:24:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2010/05/26/116339.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/116339.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2010/05/26/116339.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/116339.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/116339.html</trackback:ping><description><![CDATA[<p>今天没事用VS2005打开一个COM应用原理的源代码，一运行就报错，然后修改来修改去，最终有2个主要错误：<br>1. 不能将参数&nbsp; 从&#8220;const wchar_t [ ]&#8221;转换为&#8220;String&#8221;<br></p>
2.无法解析的外部符号 "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)&#8220;<br><br>第一个问题我发现源码中String被定义为
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">typedef&nbsp;unsigned&nbsp;</span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">String;</span></div>
翻阅MSDN里面有段话<br>
<p>By providing overloads for both the <strong>unsigned short</strong> and<strong> __wchar_t</strong> variations of <strong>wchar_t</strong>, you can create libraries that can easily be linked with code compiled with or without <strong>/Zc:wchar_t</strong> and avoid the need to provide two different builds of the library (one with and one without <strong>/Zc:wchar_t</strong> enabled).</p>
意思是要不使用内置的wchar_t类型来避免这两个不同的库冲突<br>第二个问题也是一样的<br>具体原因是你导入的库对于wchar_t不是内置类型，以前的vc6都不是，而vc6.0之后的工程缺省都是wchar_t是内建类型，其导出出来的符号是不一样的。所以，你只需要在vc7的project/proterties里面的c／c++&nbsp;&nbsp; --&gt;language&nbsp;&nbsp;&nbsp;&nbsp; --&gt;treat&nbsp;&nbsp; wchar_t&nbsp;&nbsp; as&nbsp;&nbsp; build_in&nbsp;&nbsp; type设置为no,就可以和以前那些库兼容了。所有使用了wchat_t的函数都会有这个问题。<br>
<img src ="http://www.cppblog.com/koople/aggbug/116339.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2010-05-26 05:24 <a href="http://www.cppblog.com/koople/archive/2010/05/26/116339.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于RTP中的时间戳问题</title><link>http://www.cppblog.com/koople/archive/2010/01/25/106396.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Mon, 25 Jan 2010 08:16:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2010/01/25/106396.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/106396.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2010/01/25/106396.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/106396.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/106396.html</trackback:ping><description><![CDATA[&nbsp;&nbsp; &nbsp;&nbsp; 在使用JRTPLIB的发送数据的时候需要设置时间戳单位（timestamp）和时间戳增量（timestamp increment）。看了网上一些文章，细细想来现在才想通这个问题。&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RFC3550对时间戳的描述是：<br>&nbsp;
<p style="COLOR: #ff0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 时间戳（timestamp） 32比特 时间戳反映了RTP数据包中第一个字节的采样时间。（采样时钟必须来源于一个及时的单调、线性递增时钟，以便允许同步和去除网络引起的数据包抖动（见章节6.4.1）。该时钟的分辨率必须满足理想的同步精度和测量数据包到来时的抖动的需要（一种典型的时钟分辨率不满足情况是每个视频帧仅一个时钟周期）时钟频率依赖于负载数据的格式，并在描述文件（profile）中或者是在负载格式描述中（payload format speci_cation）进行静态描述。也可以通过非RTP方法（non-RTP means）对负载格式动态描述。</p>
<p style="COLOR: #ff0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果RTP包是周期性产生的，那么将使用由采样时钟决定的名义上的采样时刻，而不是读取系统时间。例如，对一个固定速率的音频，采样时钟（时间戳时钟）将在每个周期内增加1。如果一个音频从输入设备中读取含有160个采样周期的块，那么对每个块，时间戳的值增加160，而不考虑该块是否用一个包传递或是被丢弃。</p>
<p><span style="COLOR: #ff0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 时间戳的初始值应当是随机的，就像序号一样。几个连续的RTP包如果（逻辑上）是同时产生的，如：属于同一个视频帧的RTP包，将有相同的序列号。如果数据并不是以它采样的顺序进行传输，那么连续的RTP包可以包含不是单调递增（或递减）的时间戳（RTP包的序列号仍然是单调变化的）。</span></p>
&nbsp;&nbsp;<br>根据一些文章我自己推敲了一下几个概念如下：&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 时间戳单位：时间戳计算的单位不为秒之类的单位，而是由采样频率所代替的单位，这样做的目的就是为了是时间戳单位更为精准。比如说一个音频的采样频率为8000HZ，那么我们可以把时间戳单位设为1/8000。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 时间戳增量：相邻两个RTP包之间的时间差（以时间戳单位为基准）。<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如何设定时间戳之间的增量呢？<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 按照刚才时间戳单位来看，1秒钟按照时间戳单位就是8000，那么一秒钟如果可以播放20帧，也就是发送30帧（帧率），那么可以求出相邻两帧之间的时间差，也就是时间戳增量，那么显而易见是用8000/20，那么这个时间戳增量就为400.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 网上大多数列举的一个例子是：<span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: medium Simsun; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span><span style="TEXT-ALIGN: left; LINE-HEIGHT: 23px; FONT-FAMILY: simsun; COLOR: #ff0000; FONT-SIZE: 14px" class=Apple-style-span>&nbsp;</span></span><span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: medium Simsun; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span><span style="TEXT-ALIGN: left; LINE-HEIGHT: 23px; FONT-FAMILY: simsun; COLOR: #ff0000; FONT-SIZE: 14px" class=Apple-style-span>例如MPEG，每帧20ms，采样频率8000Hz，设定时间戳单位1/8000，而每个包之间就是160的增量<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=#000000 size=3 face="Times New Roman">这里又该如何理解呢？可以轻易地看出增量是直接8000与20ms相乘的结果，我们可以知道这里两帧之间的时间为20ms，也就是0.02s，这个单位是以秒来衡量的，那么我们要用时间戳单位来表示那么就是8000*0.02=160.所以时间戳增量为160.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 还有一点为什么一般都用90000作为视频采样频率呢？<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: medium Simsun; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span><span style="TEXT-ALIGN: left; FONT-FAMILY: ����, Tahoma; FONT-SIZE: 12pt" class=Apple-style-span>90k是用于视频同步的时间尺度(TimeScale),就是每秒90k个时钟tick。为什么采用90k呢？目前视频的帧速率主要有25fps、29.97fps、30fps等，而90k刚好是它们的倍数，所以就采用了90k。<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></font></span></span>
<img src ="http://www.cppblog.com/koople/aggbug/106396.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2010-01-25 16:16 <a href="http://www.cppblog.com/koople/archive/2010/01/25/106396.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>FormatMessage</title><link>http://www.cppblog.com/koople/archive/2009/12/03/102367.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Wed, 02 Dec 2009 20:25:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2009/12/03/102367.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/102367.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2009/12/03/102367.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/102367.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/102367.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: FormatMessage函数在《windows核心编程》中第一个示例便是它的使用。这个函数我用了几次之后终于明白它的用法；这个函数是用来格式化消息字符串，就是处理消息资源的。消息资源是由mc.exe编译的，详细请在msdn中搜索mc.exe。先来看下它的函数原型DWORD&nbsp;WINAPI&nbsp;FormatMessage(&nbsp;&nbsp;__in&...&nbsp;&nbsp;<a href='http://www.cppblog.com/koople/archive/2009/12/03/102367.html'>阅读全文</a><img src ="http://www.cppblog.com/koople/aggbug/102367.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2009-12-03 04:25 <a href="http://www.cppblog.com/koople/archive/2009/12/03/102367.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于分流器那篇文章中不懂得地方</title><link>http://www.cppblog.com/koople/archive/2009/11/27/102097.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Fri, 27 Nov 2009 13:34:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2009/11/27/102097.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/102097.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2009/11/27/102097.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/102097.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/102097.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在分流器那篇文章中说到窗口子类化什么的就造成了要使用不同的宏，我一直没看明白为什么说在对话框中要那样做.<br>原文地址<br><a href="http://blog.csdn.net/hopkins9961629/archive/2006/01/25/588184.aspx"><u><font color=#800080>http://blog.csdn.net/hopkins9961629/archive/2006/01/25/588184.aspx</font></u></a><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 原文不懂的地方如下：<br><br><span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: medium Simsun; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span>
<p style="COLOR: #ff00ff; FONT-SIZE: 10pt">以上四重，是消息分离器的基本使用，但，这不完整，消息分离器主要应用在对话框消息处理中。<br>这里，窗口子类化是我们经常使用的手段，这也可以通过消息分流器实现，</p>
<p style="COLOR: #ff00ff; FONT-SIZE: 10pt">第五重<br>LRESULT CALLBACK Dlg_Proc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)<br>{<br>&nbsp;switch(msg)<br>&nbsp;{<br>&nbsp;&nbsp;HANDLE_MSG(hwnd, WM_INITDIALO , Cls_OnInitDialog); // 不能直接使用HANDLE_MSG宏<br>&nbsp;&nbsp;HANDLE_MSG(hwnd, WM_COMMAND, Cls_OnCommand); // 不能直接使用HANDLE_MSG宏<br>&nbsp;}<br>&nbsp;return false;<br>}&nbsp;<br>由于是窗口子类化，所以，最后，返回的是false，以表明，如果没有约定响应的消息，<br>则返回父亲窗口false，如果有，则返回ture，这是与前四重不同的地方。<br>一般情况下，对话框过程函数应该在处理了消息的情况下返回TRUE，如果没有处理，则返回FALSE。<br>如果对话框过程返回了FALSE，那么对话框管理器为这条消息准备默认的对话操作。</p>
<p style="COLOR: #ff00ff; FONT-SIZE: 10pt">但是，这其中有错误，因为有的消息,需要单独处理。单独处理的消息列表见SetDlgMsgResult宏。</p>
<blockquote style="MARGIN-RIGHT: 0px" dir=ltr>
<p><br><span style="FONT-SIZE: 14pt">&nbsp; 为什么不能使用？<br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; 查阅msdn发现在对话框的过程函数和window窗口有个不一样地方是在返回值上面。如果消息被处理，那么它将返回true，而如果没有处理，那么将返回false，让对话框管理器（DialogBox Manager）处理。如果想有自己的返回值的话，那么使用子类化SetWindowLong函数中的DWL_MSGRESULT标识符改变，但是之后还是要返回true。但是有一些消息不遵守上诉约定，而直接返回他们的返回值，这些消息如下：<br></span></p>
</blockquote>
<li><span style="FONT-SIZE: 10pt"><mshelp:link tabIndex=0 keywords="_win32_WM_CHARTOITEM" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><font color=#0000ff>WM_CHARTOITEM</font></u></mshelp:link> </span>
<li><span style="FONT-SIZE: 10pt"><mshelp:link tabIndex=0 keywords="_win32_WM_COMPAREITEM" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><font color=#0000ff>WM_COMPAREITEM</font></u></mshelp:link> </span>
<li><span style="FONT-SIZE: 10pt"><mshelp:link tabIndex=0 keywords="_win32_WM_CTLCOLORBTN" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><font color=#0000ff>WM_CTLCOLORBTN</font></u></mshelp:link> </span>
<li><span style="FONT-SIZE: 10pt"><mshelp:link tabIndex=0 keywords="_win32_WM_CTLCOLORDLG_cpp" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><font color=#0000ff>WM_CTLCOLORDLG</font></u></mshelp:link> </span>
<li><span style="FONT-SIZE: 10pt"><mshelp:link tabIndex=0 keywords="_win32_WM_CTLCOLOREDIT" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><font color=#0000ff>WM_CTLCOLOREDIT</font></u></mshelp:link> </span>
<li><span style="FONT-SIZE: 10pt"><mshelp:link tabIndex=0 keywords="_win32_WM_CTLCOLORLISTBOX" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><font color=#0000ff>WM_CTLCOLORLISTBOX</font></u></mshelp:link> </span>
<li><span style="FONT-SIZE: 10pt"><mshelp:link tabIndex=0 keywords="_win32_WM_CTLCOLORSCROLLBAR" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><font color=#0000ff>WM_CTLCOLORSCROLLBAR</font></u></mshelp:link> </span>
<li><span style="FONT-SIZE: 10pt"><mshelp:link tabIndex=0 keywords="_win32_WM_CTLCOLORSTATIC" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><font color=#0000ff>WM_CTLCOLORSTATIC</font></u></mshelp:link> </span>
<li><span style="FONT-SIZE: 10pt"><mshelp:link tabIndex=0 keywords="_win32_WM_INITDIALOG_cpp" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><font color=#0000ff>WM_INITDIALOG</font></u></mshelp:link> </span>
<li><span style="FONT-SIZE: 10pt"><mshelp:link tabIndex=0 keywords="_win32_WM_QUERYDRAGICON_cpp" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><font color=#0000ff>WM_QUERYDRAGICON</font></u></mshelp:link> </span>
<li><mshelp:link tabIndex=0 keywords="_win32_WM_VKEYTOITEM" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"><u><span style="COLOR: #0000ff; FONT-SIZE: 10pt">WM_VKEYTOITEM</span></u></mshelp:link>
<p><br>了解了这些之后看看那些定义的分流器宏<br>消息处理宏：</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;HANDLE_MSG(hwnd,&nbsp;message,&nbsp;fn)&nbsp;&nbsp;&nbsp;&nbsp;\</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;(message):&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;HANDLE_##message((hwnd),&nbsp;(wParam),&nbsp;(lParam),&nbsp;(fn))</span></div>
<p><br>WM_COMMAND宏的消息分流器定义：</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;HANDLE_WM_COMMAND(hwnd,&nbsp;wParam,&nbsp;lParam,&nbsp;fn)&nbsp;\</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;((fn)((hwnd),&nbsp;(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">)(LOWORD(wParam)),&nbsp;(HWND)(lParam),&nbsp;(UINT)HIWORD(wParam)),&nbsp;</span><span style="COLOR: #000000">0L</span><span style="COLOR: #000000">)</span></div>
</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我们可以看到如果在对话框的过程函数中直接使用HANDLE_MSG的话，我们使用WM_COMMAND消息，那么根据HANDLE_WM_COMMAND宏，我们可以知道它会返回0L，0在windef.h中定义为false，那么我们明明处理这个wm_command消息，但是我们告诉对话框管理器是没有处理，让对话框管理器处理。所以如果这里直接使用HANDLE_MSG是不可行的，那么我们就使用一个宏改进它<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetDlgMsgResult(hwnd,&nbsp;msg,&nbsp;result)&nbsp;((&nbsp;\</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_CTLCOLORMSGBOX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_CTLCOLOREDIT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_CTLCOLORLISTBOX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_CTLCOLORBTN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_CTLCOLORDLG&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_CTLCOLORSCROLLBAR&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_CTLCOLORSTATIC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_COMPAREITEM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_VKEYTOITEM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_CHARTOITEM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_QUERYDRAGICON&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(msg)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;WM_INITDIALOG&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">&nbsp;(BOOL)(result)&nbsp;:&nbsp;(SetWindowLongPtr((hwnd),&nbsp;DWLP_MSGRESULT,&nbsp;(LPARAM)(LRESULT)(result)),&nbsp;TRUE))<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
这样就可以解决这个问题了<br>像Jeffrey Richter 高手自己也定义了一个宏：<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;chHANDLE_DLGMSG(hWnd,&nbsp;message,&nbsp;fn)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;(message):&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(SetDlgMsgResult(hWnd,&nbsp;uMsg,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HANDLE_##message((hWnd),&nbsp;(wParam),&nbsp;(lParam),&nbsp;(fn))))</span></div>
这样可以更方便的在对话框过程函数中使用消息分流器了！ </li>
<img src ="http://www.cppblog.com/koople/aggbug/102097.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2009-11-27 21:34 <a href="http://www.cppblog.com/koople/archive/2009/11/27/102097.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>