??xml version="1.0" encoding="utf-8" standalone="yes"?>久久国产热精品波多野结衣AV ,奇米综合四色77777久久,日韩人妻无码一区二区三区久久 http://www.shnenglu.com/liyuxia713/category/10183.htmly跚前行?/description>zh-cnSun, 06 Jun 2010 18:21:37 GMTSun, 06 Jun 2010 18:21:37 GMT60内存理http://www.shnenglu.com/liyuxia713/archive/2010/06/05/117211.htmlq运?/dc:creator>q运?/author>Sat, 05 Jun 2010 01:34:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2010/06/05/117211.htmlhttp://www.shnenglu.com/liyuxia713/comments/117211.htmlhttp://www.shnenglu.com/liyuxia713/archive/2010/06/05/117211.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/117211.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/117211.html扩展阅读

内存的三U分配方式:

1Q?nbsp;从静态存储区分配Q此时的内存在程序编译的时候已l分配好Qƈ且在E序的整个运行期间都存在。全局变量Qstatic变量{在此存储?/p>

2Q?nbsp;在栈区分配:相关代码执行时创建,执行l束时被自动释放。局部变量在此存储。栈内存分配q算内置于处理器的指令集中,效率高,但容量有限?/p>

3Q?nbsp;在堆区分配:动态分配内存。用new/malloc时开辟,delete/free旉放。生存期qh定,灉|。但有内存泄露等问题?/p>

 

常见内存错误及对{?/p>

1Q?nbsp;内存分配未成功,却被使用?/p>

对策Q用内存之前检查是否分配成功。用p!=NULL判断?/p>

2Q?nbsp;内存分配成功Q未初始化就被用?/p>

内存的缺省值没有统一的标准。大部分~译器以0作ؓ初始|但不完全是?/p>

对策Q内存初始化时赋初倹{?/p>

3Q?nbsp;内存操作界?/p>

对策Q只能是心了?/p>

4Q?nbsp;释放了内存,仍然使用?/p>

Q?Q?nbsp;      使用昄delete和free的野指针?/p>

对策Q释攑֮内存Q将指针|ؓNULL?/p>

Q?Q?nbsp;      使用隐式delete和free的野指针。主要是指函数返回指向栈内存的指针或引用?/p>

对策Q当然是不要q回可以了?/p>

5Q?nbsp;未释攑ֆ存,D内存泄露?/p>

用new/malloc开辟了内存Q没用delete/free释放.

对策Qnew和delete的个C定相同;malloc和free的个C定相同;new[]和[]delete一定对应?br>

CZ1Q返回指向栈I间的指?br>

 1char* test1()
 2{
 3    char str[] = "Hello World!";
 4    return str;
 5}

 6
 7char* test2()
 8{
 9    char *str = "Hello World!";
10    return str;
11}

12
13char* test3()
14{
15    static char str[] = "Hello World!";
16    return str;
17}

18
19void main()
20{
21    char *str = NULL;
22    
23        str = test1(); 
24    cout << str << endl; //垃圾信息
25    
26    str = test2(); 
27    cout << str << endl; //ok
28    //str[1] = 'A' ; //error.试图修改常字W串
29         //str = NULL; //error.试图修改常字W串
30
31    str = test3();
32    cout << str << endl;
33}

34

 

输出l果Q?br>q

Hello World!

Hello World!

CZ2Q?/span>new?/span>delete虽然对应Q但delete释放不成?/span>

void main()
{
    
char* p = new char[4];

    p 
= "ppp";

    delete []p;
}

q行旉误?/span>P虽然是动态开辟的内存Q但在第二条语句?/span>p已经指向了静态存储区上的地址Q而对指向静态存储区的指针是不能?/span>delete释放的。此时不仅运行时错误Q还有内存泄霌Ӏ?/span>



]]>
sscanfhttp://www.shnenglu.com/liyuxia713/archive/2010/04/25/113530.htmlq运?/dc:creator>q运?/author>Sun, 25 Apr 2010 12:13:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2010/04/25/113530.htmlhttp://www.shnenglu.com/liyuxia713/comments/113530.htmlhttp://www.shnenglu.com/liyuxia713/archive/2010/04/25/113530.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/113530.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/113530.htmlint sscanf(const char* str, const char* format, ...)
functions: Reads data from str and stores them according to the parameter format into the locations given by the additional arguments. Locations pointed by each additional argument are filled with their corresponding type of value specified in the format string.

In the format:
Whitespace character: the function will read and ignore any whitespace characters which are encounterd before the next non-whitespace character.
Non-whitespace character: except percentage signs(%): any character that is not either a whitespace character or part of a format specifier causes the function to read the next character from str, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format and str. If the character does not match, the function fails and returns.
Return Value:
On success. the function returns the number of items successfully read.
On failure: In the case of an input failure before any data could be successfully read, EOF is returnded.
   //默认以空格分隔strZ同字W串,q回正确输入的变量个?nbsp;
   char buf1[512= {0};
   cout  
<< sscanf("abc def gh","%s",buf1) <<endl;   //1
   cout << buf1 << endl; //abc 
   
   
//可以使多个一起存?nbsp;
   char buf2[512= {0};
   
char buf3[512= {0};
   cout 
<< sscanf("abc 2def gh","%s%s",buf2,buf3) <<endl;  //2
   cout << buf2 << endl; //abc 
   cout << buf3 << endl;//2def
   
   
//I格或format格式以外的字W如果匹配则与前一个字W串一赯入,否则停止Q退?nbsp;
   char buf4[512= {0};
   
char buf5[512= {0};
   cout 
<< sscanf("abc 2def gh","%s,%s",buf4,buf5) <<endl; //1   
   cout << buf4 << endl; //abc
   cout << buf5 << endl; //nothing

   
char buf6[512= {0};
   
char buf7[512= {0};
   
//注意两个%s之间的空g可少  
   sscanf("abc 2def gh","%s 2%s",buf6,buf7);   
   cout 
<< buf6 << endl; //abc
   cout << buf7 << endl; //def   
   
   
//当然不只是字W串形式的,其他也都可以 
   char buf8[512= {0};
   
int a;
   cout 
<< sscanf("abc 2def gh","%*s %d",&a) <<endl;//1
   cout << a << endl;//2
   
   
//Failure 
   int b;
   cout 
<< sscanf("abc","%d",&b) <<endl; //0


]]>
sprintfhttp://www.shnenglu.com/liyuxia713/archive/2010/04/25/113525.htmlq运?/dc:creator>q运?/author>Sun, 25 Apr 2010 11:23:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2010/04/25/113525.htmlhttp://www.shnenglu.com/liyuxia713/comments/113525.htmlhttp://www.shnenglu.com/liyuxia713/archive/2010/04/25/113525.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/113525.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/113525.htmlhttp://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
 

Portotype:  int printf(char* str, const char* format, parameters);

Writes into the array pointed by str a C string consisting on a sequence of data formatted as the format argument specifies. After the format parameter, the function expects at least as many additional arguments as specified in format.

This function behaves exactly as printf does, but writing its result to a string instead of stdout. The size of the array passed as str should be enough to contain the entire formatted string .

Return value:

On success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string.

//Success
//The size of str is long enough
//the number of additional number match with the format
const int size = 25;
char *str = new char[size]; 

   //same as int flag1 = sprintf(str,"%s is written to str.","Test","tEST");
int flag1 = sprintf(str,"%s is written to str.","Test");
//Console: 23-Test is written to str
cout << flag1 << "-" << str << endl; 
On failure, a negative number is returned.
//Failure1
//additional arguments numbers is less than specified is format
//the second %s transmited as unrecognizable words
const int size = 25;
char *str = new char[size]; 
int flag2 = sprintf(str,"%s %s tttttttttttt","Test");
//Console: 19--Test @ tttttttttttt
cout << flag2 << "--" << str << endl;

//Failure2:the size of str is not long enough
   // 在dev c++不能q行Qvc6.0沒有问题

const int size = 25;
char *str = new char[size]; 
int flag3 = sprintf(str,"%s jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj","Test");
//VC6.0 Console: Test jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
//Dev c++: Console: the same as vc6.0 but throws an cannot read memory exception
cout << flag3 << "--" << str << endl;

没有试Z么时候出错返回负值呢Q!谁给我一个例子?


]]>
指针学习4--合成析构函数做了什?/title><link>http://www.shnenglu.com/liyuxia713/archive/2010/04/19/112952.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Mon, 19 Apr 2010 00:49:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2010/04/19/112952.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/112952.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2010/04/19/112952.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/112952.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/112952.html</trackback:ping><description><![CDATA[<span style="FONT-FAMILY: 微Y雅黑">与复制构造函数和复制操作W不同,~译器L提供合成析构函数?br><br>合成析构函数做什么?<br>按对象创建时的逆序撤销每个非static成员Q包括指针成员?br>只不q不删除指针成员所指向的对象?/span> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/112952.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2010-04-19 08:49 <a href="http://www.shnenglu.com/liyuxia713/archive/2010/04/19/112952.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>指针学习3----何时需要自行定义析构函?/title><link>http://www.shnenglu.com/liyuxia713/archive/2010/04/18/112928.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Sun, 18 Apr 2010 11:59:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2010/04/18/112928.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/112928.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2010/04/18/112928.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/112928.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/112928.html</trackback:ping><description><![CDATA[<span style="FONT-FAMILY: courier new">当在cȝ构造函C中申请了资源(如内存空?Q需要在对象被销毁时q行释放时? <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Test<br><img id=Codehighlighter1_11_148_Open_Image onclick="this.style.display='none'; Codehighlighter1_11_148_Open_Text.style.display='none'; Codehighlighter1_11_148_Closed_Image.style.display='inline'; Codehighlighter1_11_148_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_11_148_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_11_148_Closed_Text.style.display='none'; Codehighlighter1_11_148_Open_Image.style.display='inline'; Codehighlighter1_11_148_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_11_148_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_11_148_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    Test(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> ptr)<br><img id=Codehighlighter1_45_97_Open_Image onclick="this.style.display='none'; Codehighlighter1_45_97_Open_Text.style.display='none'; Codehighlighter1_45_97_Closed_Image.style.display='inline'; Codehighlighter1_45_97_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_45_97_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_45_97_Closed_Text.style.display='none'; Codehighlighter1_45_97_Open_Image.style.display='inline'; Codehighlighter1_45_97_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_45_97_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_45_97_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ptr)<br><img id=Codehighlighter1_59_93_Open_Image onclick="this.style.display='none'; Codehighlighter1_59_93_Open_Text.style.display='none'; Codehighlighter1_59_93_Closed_Image.style.display='inline'; Codehighlighter1_59_93_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_59_93_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_59_93_Closed_Text.style.display='none'; Codehighlighter1_59_93_Open_Image.style.display='inline'; Codehighlighter1_59_93_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>        </span><span id=Codehighlighter1_59_93_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_59_93_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>            p </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">[strlen(ptr)];<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>        }</span></span><span style="COLOR: #000000">    <br><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">Test()<br><img id=Codehighlighter1_109_126_Open_Image onclick="this.style.display='none'; Codehighlighter1_109_126_Open_Text.style.display='none'; Codehighlighter1_109_126_Closed_Image.style.display='inline'; Codehighlighter1_109_126_Closed_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_109_126_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_109_126_Closed_Text.style.display='none'; Codehighlighter1_109_126_Open_Image.style.display='inline'; Codehighlighter1_109_126_Open_Text.style.display='inline';" src="http://www.shnenglu.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </span><span id=Codehighlighter1_109_126_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.shnenglu.com/Images/dot.gif"></span><span id=Codehighlighter1_109_126_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>        delete[] p;<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</span></span><span style="COLOR: #000000"><br><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> p;<br><img src="http://www.shnenglu.com/Images/OutliningIndicators/InBlock.gif" align=top><br><img src="http://www.shnenglu.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">;</span></div> </span> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/112928.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2010-04-18 19:59 <a href="http://www.shnenglu.com/liyuxia713/archive/2010/04/18/112928.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>指针学习2--内存泄露http://www.shnenglu.com/liyuxia713/archive/2010/04/18/112897.htmlq运?/dc:creator>q运?/author>Sun, 18 Apr 2010 04:06:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2010/04/18/112897.htmlhttp://www.shnenglu.com/liyuxia713/comments/112897.htmlhttp://www.shnenglu.com/liyuxia713/archive/2010/04/18/112897.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/112897.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/112897.htmlQ?
 1#include <iostream>
 2using namespace std; 
 3
 4class Stu
 5{
 6   public:
 7         Stu(int m):var(m)
 8         {                  
 9                  cout << var <<" constructor called." << endl;
10         }
      
11          ~Stu() { cout << var << " destructor called." << endl;}         
12   private:
13          int var;            
14}
;
15
16
17int main()
18{
19    Stu *= new Stu(20); 
20    Stu *= new Stu(30);  
21    delete b;
22    return 0;
23    //or 其他隐藏异常
24
25    //D内存泄露
26    delete a;
27
28    return 0;
29}


]]>
指针学习1--new &delete & destructorhttp://www.shnenglu.com/liyuxia713/archive/2010/04/18/112892.htmlq运?/dc:creator>q运?/author>Sun, 18 Apr 2010 02:58:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2010/04/18/112892.htmlhttp://www.shnenglu.com/liyuxia713/comments/112892.htmlhttp://www.shnenglu.com/liyuxia713/archive/2010/04/18/112892.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/112892.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/112892.html1. 一般指针的new&delete

对于指针a,delete a之后Q指?/span>a 的地址仍然是原来的地址(q不是NULL)Q只不过所指向的对象被释放了,此时指针存放的gؓ随机的,q译器定?br>

 1     int *= new int(2);
 2     cout << "after new :" << endl;
 3     cout << "a = " << a << "," << "*a = " << *<< endl;
 4
 5     delete a;
 6     cout << "after delete: " << endl;
 7     if(a == NULL)
 8     {
 9         cout << "a is null after delete" << endl;
10     }

11     else
12     {
13         cout << " a is not null after delete" << endl;
14     }

15     cout << "a = " << a << "," << "*a = " << *<< endl; 
//好的~程习惯
delete a;
= NULL;

常规Ҏ(gu)创徏的对象,当实际对象(而不是对象的引用Q超Z用域Ӟ才会q行析构函数
动态方法创建的对象Q当删除指向动态分配对象的指针Ӟ才会q行析构函数?nbsp;        


2. 一般类对象的声明与初始化:不用new也可以定义类对象Q区别于javaQ,默认调析构函?br>注:为方便简单演C,q没有遵循三法则
 1class Stu
 2{
 3   public:
 4         Stu(int m):var(m)
 5          
 6                  var = m;
 7                  cout << "constructor called." << endl;
 8          }
      
 9          ~Stu() { cout << var << " destructor called." << endl;}         
10   private:
11          int var;         
12}
;
13
14void func()
15{
16       //调用构造函?/span>
17       Stu a(20);
18       //调用默认复制构造函?/span>
19       Stu b(a);     
20       //{h(hun)于Stu temp(b); Stu b(temp);  
21       //所以有调用构造函数的q程
22       Stu c = 30;     
23       //都是用隐式方式定义的Q所以不用delete,自动调用析构函数
24 |        //注意析构的顺?nbsp;    
25}

26
27int main()
28{
29   func();   
30   return 0;
31}

q里有一个需要注意的地方Q就是如果func里面的代码是直接写在main里的则不一定调用析构函?nbsp;Q由~译器决定?br>上面是dev c++, 下面是vc6.0的运行结?br>
 1
 2int main()
 3{
 4
 5    //{
 6       //调用构造函?/span>
 7       Stu a(20);
 8       //调用默认复制构造函?/span>
 9       Stu b(a);     
10       //{h(hun)于Stu temp(b); Stu b(temp);  
11       //所以有调用构造函数的q程
12       Stu c = 30;     
13       //new出来的指针对象,必须昄delete,如a,b
14       //cd象结束局部范围后会自动调用析构函?nbsp;如c    
15    //}
16    return 0;
17}


3. 用new定义cd?必须delete
动态分配的对象只有在指向该对象的指针被删除时才撤销。如果没有删除指向动态对象的指针Q则不会q行该对象的析构函数Q对象则一直存在,从而导致内存泄霌Ӏ?br>常规Ҏ(gu)创徏的对象,当实际对象(而不是对象的引用Q超Z用域Ӟ才会q行析构函数
动态方法创建的对象Q当删除指向动态分配对象的指针Ӟ才会q行析构函数?br>
 1class Stu
 2{
 3   public:
 4         Stu(int m):var(m)
 5          
 6                  var = m;
 7                  cout << "constructor called." << endl;
 8          }
      
 9          ~Stu() { cout << var << " destructor called." << endl;}         
10   private:
11          int var;         
12}
;
13
14void func()
15{
16       //调用构造函?/span>
17       Stu* a = new Stu(20);
18       //调用默认复制构造函?/span>
19       Stu* b= new Stu(*a);     
20       //{h(hun)于Stu temp = new Stu(30); Stu b(temp);  
21       //所以有调用构造函数的q程
22       Stu c = 30;     
23       //new出来的指针对象,必须昄delete,如a,b
24       //cd象结束局部范围后会自动调用析构函?nbsp;如c
25       delete a;
26       delete b;
27}

28
29int main()
30{
31   func();   
32   return 0;
33}


]]>
宏与内联(inline)的区?转蝲)http://www.shnenglu.com/liyuxia713/archive/2010/03/04/108897.htmlq运?/dc:creator>q运?/author>Thu, 04 Mar 2010 12:22:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2010/03/04/108897.htmlhttp://www.shnenglu.com/liyuxia713/comments/108897.htmlhttp://www.shnenglu.com/liyuxia713/archive/2010/03/04/108897.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/108897.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/108897.html先{载下人家ȝ的宏和普通函数调用的区别Q?br>(1)、宏只做单的字符串替换,函数是参C递,所以必然有参数cd?支持各种cdQ而不是只有字W串)?br>(2)、宏不经计算而直接替换参敎ͼ函数调用则是参数表辑ּ求值再传递给形参?br>(3)、宏在编译前q行Q即先替换再~译。而函数是~译后,在执行时才调用的。宏占编译时_而函数占执行旉?br>(4)、宏参数不占I间Q因为只做字W串替换Q而函数调用时参数传递是变量之间的传递,形参作ؓ局部变量占内存I间?br>(5)、函数调用需要保留现场,然后转入调用函数执行Q执行完毕再q回主调函数Q这些耗费在宏中是没有的?/p>


使用宏和内联函数都可以节省在函数调用斚w的时间和I间开销。二者都是ؓ了提高效率,但是却有着显著的区别:
(1)、在使用Ӟ宏只做简单的预处理器W号?字符?中的单替换。而内联函数可以进行参数类型检查,且具有返回?也能被强制{换ؓ可{换的合适类??br>(2)、内联函数首先是函数Q函数的许多性质都适用于内联函?如内联函数可以重??br>(3)、内联函数可以作为某个类的成员函敎ͼq样可以使用cȝ保护成员和私有成员。而当一个表辑ּ涉及到类保护成员或私有成员时Q宏׃能实C(无法this指针攑֜合适位|??/p>


可以用内联函数完全替代宏?br>但是在用内联函数时也要注意Q作为内联函敎ͼ函数体必d分简单,不能包含循环Q条Ӟ选择{复杂结构,否则不能作ؓ内联函数?br>实际上,~译器的优化pȝ会自动将一些简单函数变成内联函数。而一些复杂的函数Q即使指定ؓ内联Q编译器也会自动当作普通函数?/p>

 

文章出处QDIY部落(http://www.diybl.com/course/3_program/c++/cppxl/20081216/154041.html)



]]>
随机数的生成http://www.shnenglu.com/liyuxia713/archive/2010/01/21/106136.htmlq运?/dc:creator>q运?/author>Thu, 21 Jan 2010 07:37:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2010/01/21/106136.htmlhttp://www.shnenglu.com/liyuxia713/comments/106136.htmlhttp://www.shnenglu.com/liyuxia713/archive/2010/01/21/106136.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/106136.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/106136.htmlrand() 可获得伪随机数。但是仅仅这样写的话Q同一D代码每ơ运行程序获得的随机数相同?br>
因ؓ在调用rand()前自动调用了srand(1); q条语句?只有当srand()的Ş参是变化的时每次q行获得的随机数才不同?br>
用什么样的随时变化的参数呢? 用time(NULL),  即srand((unsigned)time(NULL)); rand(); q样每次q行得到的随机数不同?br>
如何获得某个区间的随机数Q?br>      ---- 如果是区间[0,n),   rand()%n 可以了
      ---- 如果是区间[m,n),  rand()%n+m 可以了
     ----如果是区[0,1), rand()/double(RAND_MAX) 可以可以了?RAND_MAX在cstdlib里有定义)

q里有更详细的介l:
http://blog.csdn.net/zhoubl668/archive/2009/01/04/3704604.aspx

]]>
cL员函数承(virtual、非virtualQ?/title><link>http://www.shnenglu.com/liyuxia713/archive/2010/01/08/105170.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Fri, 08 Jan 2010 08:30:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2010/01/08/105170.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/105170.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2010/01/08/105170.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/105170.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/105170.html</trackback:ping><description><![CDATA[     摘要: <br>?对于父类函数Qvirtual、非virtualQ,如果子类没有同名函数Q则正常l承 <br> <br>?对于父类函数Qvirtual、非virtualQ,如果子类有同名函敎ͼ无同型函敎ͼ则不能调用父cd?<br> <br>?对于父类函数Qvirtual、非virtualQ,如果有同型函敎ͼ <br> <br>----非virtual函数由指针类型决定调用哪?<br> <br>----virtual函数由指针指向的对象军_调用哪个Q运行时军_Q?<br>  <a href='http://www.shnenglu.com/liyuxia713/archive/2010/01/08/105170.html'>阅读全文</a><img src ="http://www.shnenglu.com/liyuxia713/aggbug/105170.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2010-01-08 16:30 <a href="http://www.shnenglu.com/liyuxia713/archive/2010/01/08/105170.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>cstatic成员http://www.shnenglu.com/liyuxia713/archive/2010/01/08/105152.htmlq运?/dc:creator>q运?/author>Fri, 08 Jan 2010 04:22:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2010/01/08/105152.htmlhttp://www.shnenglu.com/liyuxia713/comments/105152.htmlhttp://www.shnenglu.com/liyuxia713/archive/2010/01/08/105152.html#Feedback6http://www.shnenglu.com/liyuxia713/comments/commentRss/105152.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/105152.htmlc?/span>static成员引进Q有时特定类的全体对象需要访问一个全局对象?/span>

 

?/span>Z么用static成员而不是全局对象Q?/span>

       ---- static对象名字在类作用域中Q可以有效避免命名冲H,q且清晰昄E序意图?/span>

       ---- 可以实施装Q?/span>static成员可以定义?/span>privateQ而全局对象不可以)

 

?/span> 如何调用Q?/span>     ---- class A a; A::static_mem; a.static_mem;

 

?/span> 声明和定?/span>

       ---- static成员函数在类定义体内部外部定义均可。在cd义体外定义时不加Q不可以加)static修饰?/span>

       ---- static数据成员必须在类定义体外部定义(正好一ơ)(在类定义体外声明q定?/span>)

              class A{public : static int n;} int A::n = 10;

       ---- const static数据成员可以Q也可以不)在类定义内初始化Q但必须在类定义体外部重新声明(不可以加static修饰W,不可以赋初|

 

?/span> static数据成员其他Ҏ(gu):

       ----一般地Q在cȝ内部不能有该cȝ型的变量Q或函数形参Q,最多只能有该类cd的指针或引用做变量类型(或函数Ş参).  static数据成员则不受这个限制?/span>

       ---- static数据成员可以作ؓ函数默认实参 

       ---- static成员的承:如果基类有static成员Q则整个l承层次中只有一个这L成员。每个static成员L只有一个实例?br>     

 1#include <iostream> 
 2#include <cstdlib>
 3
 4using namespace std; 
 5
 6class A
 7{
 8      //重蝲输出操作W?nbsp;
 9      friend ostream& operator<<(ostream& outconst A&a)
10      {
11             out << a.m;
12             return out;
13      }

14      
15private:
16        int m ;
17 
18public:
19       A(int i) { m = i;}
20       
21       static int n;  
22       
23       static A a; //cd以有该类cd的static成员
24       //A a1; //error. cM可以有普通的该类cd的成?nbsp;           
25       
26       static void func(A a) {cout << a << endl;} //cȝ型可以作cstatic函数的Ş?nbsp;  
27       
28       //static 数据成员可以作ؓcL员函数的默认实参 
29       static void func2(int i = n) { cout << i*<<endl;}    
30}
;
31
32int A::n = 10;
33A A::a(1);
34
35int main()
36{
37    cout << A::n << endl; //10
38    cout << A::a <<endl; //1
39    
40    A a2(3); 
41    A::func(a2); //3
42    A::func2(); //100
43 
44    system("pause");
45    return 0;
46}
 




]]>
深入理解strcpyQstrncpyhttp://www.shnenglu.com/liyuxia713/archive/2009/04/26/81155.htmlq运?/dc:creator>q运?/author>Sun, 26 Apr 2009 12:59:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2009/04/26/81155.htmlhttp://www.shnenglu.com/liyuxia713/comments/81155.htmlhttp://www.shnenglu.com/liyuxia713/archive/2009/04/26/81155.html#Feedback1http://www.shnenglu.com/liyuxia713/comments/commentRss/81155.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/81155.html阅读全文

]]>
sizeofQ(含位域)l构体内存对齐,压羃存储http://www.shnenglu.com/liyuxia713/archive/2009/04/25/80918.htmlq运?/dc:creator>q运?/author>Sat, 25 Apr 2009 03:33:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2009/04/25/80918.htmlhttp://www.shnenglu.com/liyuxia713/comments/80918.htmlhttp://www.shnenglu.com/liyuxia713/archive/2009/04/25/80918.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/80918.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/80918.html1. 一些基本类型的vc6 sizeofl果
2. l构体的内存寚w
3. 含位域的l构体介l?
4. 含位域的l构体的内存寚wQ压~存储和非压~存储)
5. 嵌套l构体的sizeof
6. cd象的sizeof
7. 通过代码如何修改默认寚w模数  阅读全文

]]>
[导入]多重l承与虚l承http://www.shnenglu.com/liyuxia713/archive/2009/03/25/79747.htmlq运?/dc:creator>q运?/author>Wed, 25 Mar 2009 08:20:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2009/03/25/79747.htmlhttp://www.shnenglu.com/liyuxia713/comments/79747.htmlhttp://www.shnenglu.com/liyuxia713/archive/2009/03/25/79747.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/79747.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/79747.htmlTechnorati 标签:

1.多重l承下的cM用域名字查找规则Q?/strong>W一步,~译器找C个匹配的声明。如果匹配的声明不止一个,则导致二义性,出错Q第二步Q编译器定扑ֈ的名字是否合法?

避免二义性的Ҏ(gu)Q在解决二义性的zcM定义函数的一个版本?

2. 采用虚承的Ҏ(gu)可以有效减少二义性?/strong>定义虚承的Ҏ(gu)Q在z列表中包?#8220;virtual”?

虚承中名字查找Ҏ(gu)。设查找函数funcQ(1Q如果在每个路径中func表示同一基类成员Q则没有二义性,因ؓcd享该成员的单个实例。(2Q如果在某个路径中func是虚基类的成员,而在另一路径上是后代zcȝ成员Q也没有二义性,因ؓ特定zcd例的优先U高于共享虚基类实例。(3Q如果沿每个l承路径func表示后代zcȝ不同成员Q则h二义性?

3.虚承的Ҏ(gu)初始化方?/strong>。通常Q每个类只初始化自己的直接基cR但在虚l承中也q样q行的话Q可能会多次初始化虚基类?

由最低层Q非虚承)zcȝ构造函数初始化虚基cR(2QQ何直接或间接l承虚基cȝcM般也必须虚基cL供自q初始化式Q以提供自n对象初始化用?

class ZooAnimal{...};

class Bear: public virtual ZooAnimal{...};

class Raccoon: public virtual ZooAnimal{...};

class Endangered{...};

class Panda: public Bear,public Raccoon,public Endangered{...};

在上面的例子中,Panda构造函数初始化PandaQZooAnimal,Bear,Raccoon,Endangered部分Q供Panda对象使用。BearQor RaccoonQ构造函数初始化Bear(or Raccoon),ZooAnimal部分供Bear(or Raccoon)对象使用.

4. 虚承的构造函数次?/strong>。先是全部直接基cȝ虚基cȝ构造函数按声明的顺序,然后是非虚基cd数按声明的顺序运行?img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%e5%a4%9a%e9%87%8d%e7%bb%a7%e6%89%bf%e4%b8%8e%e8%99%9a%e7%bb%a7%e6%89%bf&referrer=" width=1 border=0>
文章来源:http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!267.entry



]]>
[导入]模板中的一些规?/title><link>http://www.shnenglu.com/liyuxia713/archive/2009/03/25/79748.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Wed, 25 Mar 2009 06:56:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2009/03/25/79748.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/79748.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2009/03/25/79748.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/79748.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/79748.html</trackback:ping><description><![CDATA[<p><strong>1. 模板定义内的自定义类型成员调用方法(用typename昄说明Q:</strong>cd以定义类型成员,如size_type, size_t{类型。在定义模板cL如何调用它呢Qؓ了说明是cdQ显C用typename说明? <p><font color=#ff00ff>template<class cl, class T></font> <p><font color=#ff00ff>cl func(cl* cl_p, T value)</font> <p><font color=#ff00ff>{</font> <p><font color=#ff00ff>       //<font color=#ff0000>cl::size_type * value;</font> </font><font color=#000000>// If cl::size_type is a type, then a declaration</font> <blockquote> <p><font color=#000000>                        // If cl::size_type is an object, then a multiplication</font> <p><font color=#ff00ff>typename cl::size_type *value;</font></p> </blockquote> <p><font color=#ff00ff>}                            </font> <p><strong>2. 非类型模板Ş参的使用: </strong>模板非类型Ş参是模板定义内部的常量?(在需要常量表辑ּ的时?可以用非cd形参,如定义数l的长度)? <p>因ؓ非类型模板Ş参需要传递的是常量表辑ּQ所以不支持一般的隐式cd转换? <p><font color=#ff00ff>template<class T, size_t N> void fcn(T (&cl)[N]) {...}</font> <p><font color=#ff00ff>int x[20];</font> <p><font color=#ff00ff>fcn(x);</font> <p> <p><font color=#ff00ff></font><strong>3. 模板何时实例化?</strong>函数声明Q定义对象的引用和指向对象的指针都不会实例化。定义类对象或调用函数时实例化? <p><strong>4.friend模板声明依赖性:</strong>Q?Q?当授予对l定模板所有实例的讉K权时Q在作用域中不需要存在该cL板或函数模板的声明。编译器友元声明也当作cL函数的声明对待;Q?Q想要限制对特定实例化的友元关系Ӟ必须在可以用于友元声明之前声明类或函数? <p><strong>5.对于不同参数的函数模板用相同参数可以调用么?</strong>可以Q会调用隐式转换? <p><font color=#ff00ff>#include<iostream> </font> <p><font color=#ff00ff>using namespace std; </font> <p><font color=#ff00ff>template<typename T1, typename T2> </font> <p><font color=#ff00ff>void print(const T1 &v1, const T2 &v2)</font><font color=#000000>  //参数cd不同</font><font color=#ff00ff><br>{<br>    cout << "T1 = " << v1 <<endl;<br>    cout << "T2 = " << v2 <<endl;<br>} </font> <p><font color=#ff00ff>int main()<br>{<br>    print(1,2); </font><font color=#000000>//ok </font> <p><font color=#ff00ff>    int a=1;<br>    int b=2;<br>    print(a,b);</font><font color=#000000> //okQŞ参类型相?/font> <p><font color=#ff00ff>    return 0;<br>}</font><img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%e6%a8%a1%e6%9d%bf%e4%b8%ad%e7%9a%84%e4%b8%80%e4%ba%9b%e8%a7%84%e5%88%99&referrer=" width=1 border=0><img style="POSITION: absolute" height=0px alt="" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com&GT1=liyuxia-life" width=0px><br>文章来源:<a >http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!266.entry</a> </p> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/79748.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2009-03-25 14:56 <a href="http://www.shnenglu.com/liyuxia713/archive/2009/03/25/79748.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[导入]模板~译模型http://www.shnenglu.com/liyuxia713/archive/2009/03/24/79749.htmlq运?/dc:creator>q运?/author>Tue, 24 Mar 2009 07:50:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2009/03/24/79749.htmlhttp://www.shnenglu.com/liyuxia713/comments/79749.htmlhttp://www.shnenglu.com/liyuxia713/archive/2009/03/24/79749.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/79749.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/79749.html1.包含~译模型Qinclusion compilation modelQ? 函数声明攑֜头文件中Q定义放在源文g中。头文g֌含源文g。据说会出现一个模板实例化多次从而导致编译时性能显著降低?

//template.cpp
template<typename T>
void print(const T &v)
{
    cout << "T = " << v <<endl;
}

//template.h
#ifndef TEMPLATE_H
#define TEMPLATE_H

template<typename T>
void print(const T &v);

#include "template.cpp" 

#endif

//main.cpp
#include<iostream>
#include "template.h"  
//#include"template.cpp" 用这条命令代替也可以?/font>

using namespace std;

int main()
{
   
print(1); //ok
    return 0;
}

2. 分别~译模型Qseperate compilation modelQ?函数声明和类定义攑֜头文件中Q带export关键字的函数定义和类声明攑֜源文件中。源文g֌含头文g。(不知道理解的对不对,用的~译器不支持分别~译Q暂时无从判断了Q?

//the template definition goes in a separately-compiled source file

export template<typename T>

T print(const T&v) /*...*/

//class template header goes in shared header file

template <class T> class cl{...};

//cl.cpp implementation file declares cl as exported

export template <class T> class cl;

#include "cl.h"

//cl member definitions


文章来源:http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!265.entry



]]>
[导入]基类的复制控制函?/title><link>http://www.shnenglu.com/liyuxia713/archive/2009/03/23/79750.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Mon, 23 Mar 2009 11:09:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2009/03/23/79750.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/79750.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2009/03/23/79750.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/79750.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/79750.html</trackback:ping><description><![CDATA[<p>构造函C能定义ؓvirtualQ派生类对象的基cL据成员部分在初始化列表中用基cL造函数初始化Q? <p>基类析构函数应定义ؓvirtualQ复制操作符一般定义ؓ非virtual <p>在基cL造函数和析构函数中,派生类对象当作基类cd对象对待。(因ؓ在这两个函数的运行过E中Q对象不是一个完整的zcȝ型) <p>cȝ复制控制的三法则有个例外Q定义(I)虚构够函数时可以不定义构造函数和赋值函数?img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%e5%9f%ba%e7%b1%bb%e7%9a%84%e5%a4%8d%e5%88%b6%e6%8e%a7%e5%88%b6%e5%87%bd%e6%95%b0&referrer=" width=1 border=0><img style="POSITION: absolute" height=0px alt="" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com&GT1=liyuxia-life" width=0px><br>文章来源:<a >http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!264.entry</a> </p> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/79750.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2009-03-23 19:09 <a href="http://www.shnenglu.com/liyuxia713/archive/2009/03/23/79750.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[导入]zcd基类转换的可讉K?/title><link>http://www.shnenglu.com/liyuxia713/archive/2009/03/22/79751.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Sun, 22 Mar 2009 12:22:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2009/03/22/79751.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/79751.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2009/03/22/79751.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/79751.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/79751.html</trackback:ping><description><![CDATA[<p>zcLw的成员和友元Q何承时都可以用派生类到基c{换? <p>对于后代cd用户代码Q? <p>publicl承Ӟ后代cd用户代码都可以? <p>protectedl承Ӟ后代cd以,用户代码不可以? <p>privatel承Ӟ后代cd用户代码都不可以?img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%e6%b4%be%e7%94%9f%e7%b1%bb%e5%88%b0%e5%9f%ba%e7%b1%bb%e8%bd%ac%e6%8d%a2%e7%9a%84%e5%8f%af%e8%ae%bf%e9%97%ae%e6%80%a7&referrer=" width=1 border=0><img style="POSITION: absolute" height=0px alt="" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com&GT1=liyuxia-life" width=0px><br>文章来源:<a >http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!263.entry</a> </p> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/79751.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2009-03-22 20:22 <a href="http://www.shnenglu.com/liyuxia713/archive/2009/03/22/79751.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[导入]zcd基类成员的访问权?/title><link>http://www.shnenglu.com/liyuxia713/archive/2009/03/22/79752.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Sun, 22 Mar 2009 11:26:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2009/03/22/79752.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/79752.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2009/03/22/79752.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/79752.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/79752.html</trackback:ping><description><![CDATA[<p>1.zcd基类private成员没有讉K权限? <p>2.zcd能通过zcd象访问其<font color=#ff0000>基类的protected成员</font>Q派生类对其<font color=#ff0000>基类cd对象的protected成员</font>没有Ҏ(gu)讉K权限? <p><font color=#ff00ff>#include<iostream><br>using namespace std; </font> <p><font color=#ff00ff>class Base<br>{<br>public:<br>    Base():i(0),j(0){};<br>protected:<br>    int i;<br>private:<br>    int j;<br>}; </font> <p><font color=#ff00ff>class Derived:public Base<br>{<br>    Derived():Base(){}; </font> <p><font color=#ff00ff>    print(const Base &b, const Derived &d)<br>    {<br>        int num = i;<br>        <font color=#ff0000>//num = b.i;</font> <font color=#404040><font color=#000000>//error. cannot access protected member declared in class 'Base'</font><br></font>        num = d.i;<br>        <font color=#ff0000>//num = d.j;</font> <font color=#000000>//error. cannot access private member declared in class 'Base'<br></font>    };<br>}; </font> <p><font color=#ff00ff>int main()<br>{<br>    return 0;<br>}</font><img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%e6%b4%be%e7%94%9f%e7%b1%bb%e5%af%b9%e5%9f%ba%e7%b1%bb%e6%88%90%e5%91%98%e7%9a%84%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90&referrer=" width=1 border=0><img style="POSITION: absolute" height=0px alt="" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com&GT1=liyuxia-life" width=0px><br>文章来源:<a >http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!262.entry</a> </p> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/79752.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2009-03-22 19:26 <a href="http://www.shnenglu.com/liyuxia713/archive/2009/03/22/79752.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[导入]析构函数Q内存泄漏,三法则)http://www.shnenglu.com/liyuxia713/archive/2009/03/08/79754.htmlq运?/dc:creator>q运?/author>Sun, 08 Mar 2009 13:25:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2009/03/08/79754.htmlhttp://www.shnenglu.com/liyuxia713/comments/79754.htmlhttp://www.shnenglu.com/liyuxia713/archive/2009/03/08/79754.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/79754.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/79754.htmlTechnorati 标签:

http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!255.entry



]]>
[导入]Q复制、默认)构造函?/title><link>http://www.shnenglu.com/liyuxia713/archive/2009/03/08/79755.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Sun, 08 Mar 2009 13:00:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2009/03/08/79755.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/79755.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2009/03/08/79755.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/79755.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/79755.html</trackback:ping><description><![CDATA[<div style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">Technorati 标签: <a rel=tag>cd始化列表Q默认构造函敎ͼ复制构造函?/a></div> <p><font color=#ff0000>初始化列表:</font>通常使用初始化是Z提高效率Q它直接调用与实参匹配的构造函数。【因Z般在构造函C内的复制也经q初始化-->计算赋?font color=#404040>Q此时会调用复制构造函敎ͼ</font>两个阶段】;特别的有些时候只能用初始化列表,即不能赋值的参数Q如Qconst或引用类型的成员Q没有默认构造函数的cȝ型成员。初始化列表中变量的初始化顺序是先声明的先初始化. <p><font color=#ff00ff>class cl1{ private: const int ci;  int &ri;};</font><font color=#404040> //ci,ri只能在初始化列表中进行初始化?/font> <p><font color=#404040><font color=#ff0000>默认构造函敎ͼ</font></font><font color=#6600ff>全部形参是默认实参的构造函C是默认构造函数?/font> <p>只要自己定义了(L的)构造函敎ͼ~译器就不会为我们合成默认构造函数? <p><font color=#ff0000>复制构造函敎ͼ</font><font color=#6600ff>单个形参为本cȝ型对象的引用的构造函数。对于不支持复制的类型(如IOcdQ不能用复制构造函数。如果自己定义了复制构造函敎ͼ而不是构造函敎ͼ卻I只定义构造函敎ͼ但没有定义复制构造函敎ͼ则编译器合成复制构造函敎ͼQ则~译器不会合成复制构造函数。【复制构造函数需要特别注意指针成员,以后说明?/font> <p><font color=#ff00ff>ifstream file1("filename1");</font> <font color=#404040>//ok, direct initialization.</font> <p><font color=#ff00ff>ifstream file2="filename2";</font> <font color=#404040>//error</font> <p><font color=#ff0000>如何防止复制Q?/font>可以通过复制构造函数声明ؓprivate来禁止普通函敎ͼ非成员,非友元函敎ͼ的访问;可以通过声明一个private复制构造函数而不对其定义来禁止成员函数和友元函数的访问?img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%ef%bc%88%e5%a4%8d%e5%88%b6%e3%80%81%e9%bb%98%e8%ae%a4%ef%bc%89%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0&referrer=" width=1 border=0><img style="POSITION: absolute" height=0px alt="" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com&GT1=liyuxia-life" width=0px><br>文章来源:<a >http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!254.entry</a> </p> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/79755.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2009-03-08 21:00 <a href="http://www.shnenglu.com/liyuxia713/archive/2009/03/08/79755.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[导入]cȝ一些特D限制成?/title><link>http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79756.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Fri, 06 Mar 2009 13:57:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79756.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/79756.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79756.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/79756.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/79756.html</trackback:ping><description><![CDATA[<p><font color=#ff0000>inline成员函数Q?/font> 声明或定义时指定为inline都可以。作用:在调用处直接在行内展开代码Q以提高效率。类的inline成员函数定义在包含该cȝ头文件中?cd部定义的函数均ؓinline函数? <p><font color=#ff0000>mutable数据成员</font>QQ何函敎ͼ包括cconst成员函数Q都可以修改mutable数据成员? <p><font color=#ff0000>explicit构造函敎ͼ</font>声明时指定,定义时不允许重复指定为explicit。作用:防止在需要隐式{换的上下文中使用构造函数? <p><font color=#ff0000>friend成员Q?/font>非类成员可以讉KcȝU有成员。需要特别注意友元声明和作用域。如果想(其他cȝQ成员函数设为友元,必须先声明;而如果想(其他Q类或非成员函数设ؓ友元Q则不必预先声明? <p><font color=#ff0000>static数据成员Q?/font>static数据成员不用构造函敎ͼ在类的外部定义,定义时进行初始化? <p><font color=#ff0000>static成员函数Q?/font>声明时指定staticcdQ定义时不用重复声明。没有this指针? <p><font color=#ff0000>const static数据成员Q?/font>一般地cȝstatic数据成员不能在类的内部定义。有例外是可以用常量表辑ּ初始化const static数据成员Q不q即使这样也需要在cd义体的外部进行该const static数据成员的定义? <p><font color=#404040>static成员不是cd象的l成部分Q非static数据成员不能是该成员所属的cȝ型,而只能是对应的指针和引用Q而static成员则可以是该成员所属的cȝ型?/font><img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%e7%b1%bb%e7%9a%84%e4%b8%80%e4%ba%9b%e7%89%b9%e6%ae%8a%e9%99%90%e5%88%b6%e6%88%90%e5%91%98&referrer=" width=1 border=0><img style="POSITION: absolute" height=0px alt="" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com&GT1=liyuxia-life" width=0px><br>文章来源:<a >http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!252.entry</a> </p> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/79756.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2009-03-06 21:57 <a href="http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79756.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[导入]容器适配?/title><link>http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79757.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Fri, 06 Mar 2009 11:17:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79757.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/79757.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79757.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/79757.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/79757.html</trackback:ping><description><![CDATA[<blockquote> <p>queue             priority_queue         stack <p>队列                ?nbsp;                        ? <p>deque             vector                   deque <font color=#404040>//默认相关联容器类?/font> <p>push_front       随机讉K                 ?nbsp; <font color=#404040>//对关联容器的要求</font></p> </blockquote><img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%e5%ae%b9%e5%99%a8%e9%80%82%e9%85%8d%e5%99%a8&referrer=" width=1 border=0><img style="POSITION: absolute" height=0px alt="" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com&GT1=liyuxia-life" width=0px><br>文章来源:<a >http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!251.entry</a> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/79757.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2009-03-06 19:17 <a href="http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79757.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[导入]q代?/title><link>http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79758.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Fri, 06 Mar 2009 11:11:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79758.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/79758.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79758.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/79758.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/79758.html</trackback:ping><description><![CDATA[<p>感觉q代器这部分函数的返回类型是没太弄明白的Q以后用的时候得多注意点? <p><font color=#ff0000>1.插入Q?/font>一般插入是在给定的q代器位|前Q这hendq代器也课正常编译。插入的元素cd必须与c的类型完全一栗? <p>   <font color=#ff00ff>void c.push_back(t); </font> <p><font color=#ff00ff>    void c.insert(p,b,e); </font> <p><font color=#ff00ff>    void c.insert(p,n,t);</font> <p><font color=#ff00ff>    iter c.insert(p,t);</font> <p><font color=#ff0000>2.赋?/font>Q?font color=#ff00ff>c1= c2</font> <p><font color=#ff00ff>     c.assign(b,e); c.assign(n,t)</font>  <font color=#404040>//允许不同的容器,不同的元素,只要元素cd兼容?/font> <p><font color=#ff0000>3.讉KQ?/font><font color=#ff00ff>c.back(); c.front(); </font> <blockquote> <p><font color=#ff00ff>c[n]; c.at[n];</font><font color=#404040> //q样的下标访问容易越界。后者越界是抛出 out_of_range异常?/font></p> </blockquote> <p><font color=#ff0000>4.删除</font>Q?font color=#ff00ff>iter c.erase(p);</font> <blockquote> <p><font color=#ff00ff>iter c.erase(b,e);</font> <p><font color=#ff00ff>void c.clear();</font> <p><font color=#ff00ff>void c.pop_back();</font> <p><font color=#ff00ff>c.pop_front();</font></p> </blockquote><img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%e8%bf%ad%e4%bb%a3%e5%99%a8&referrer=" width=1 border=0><img style="POSITION: absolute" height=0px alt="" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com&GT1=liyuxia-life" width=0px><br>文章来源:<a >http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!250.entry</a> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/79758.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2009-03-06 19:11 <a href="http://www.shnenglu.com/liyuxia713/archive/2009/03/06/79758.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[导入]const限制W?/title><link>http://www.shnenglu.com/liyuxia713/archive/2009/02/28/79759.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Sat, 28 Feb 2009 13:38:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2009/02/28/79759.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/79759.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2009/02/28/79759.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/79759.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/79759.html</trackback:ping><description><![CDATA[<p>1. <font color=#ff0000>const变量Q?/font><font color=#ff00ff>const type v;</font> 必须定义时初始化Q不能修改其倹{?font color=#404040>备注Q关于const变量的作用域要特别注意,它是局部的Q而默认的C++变量是全局的。通过加extern限制可以令const对象可以在全局被访问。如代码Q?Z提一下static)</font> <p><font color=#404040>file1.cpp   </font><font color=#ff00ff>type v1; </font> <blockquote> <p><font color=#ff00ff>        const type v2;  </font><font color=#404040>//now,v2 is a local variable.</font> <p><font color=#ff00ff>        extern const type v3</font><font color=#404040>;//now,v3 is a global variable. must add 'extern'~. </font></p> </blockquote> <p><font color=#404040>file2.cpp   <font color=#ff00ff>type v1;</font> //  error.redeclaration.</font> <blockquote> <p><font color=#404040>        <font color=#ff00ff>extern type v1;</font> //ok.</font> <p><font color=#404040>        <font color=#ff00ff>type v2;</font> //ok.but not equivalent to v2 in file1.cpp</font> <p><font color=#404040>        <font color=#ff00ff>const type v3;</font>  // ok.but not equivalent to v3 in file1.cpp</font> <p><font color=#404040>        <font color=#ff00ff>extern const type v3;</font> //ok.equal to v3 in file1.cpp.  </font></p> </blockquote> <p>2. <font color=#ff0000>const引用Q?/font><font color=#ff00ff>const type& v;</font> 是指向const对象的引用。非const引用只能l定C该引用同cd的对象。而const引用则可以绑定到不同但相关的cd那个的对象和叛_{? <p>double dv = 1.0;  const int &iv = dv; ~译时等价于 <p>int temp = dv;  const int &iv = temp; //可以看出对iv的修改ƈ不会影响dv的? <p>3.<font color=#ff0000>const与指?/font>Q(1Q?<font color=#ff0000>指向const对象的指?</font> <font color=#ff00ff>const type* v;</font> 有时C<font color=#ff00ff>type const* v;</font>可以修改指针Q但不能直接通过该指针修Ҏ(gu)针指向的对象。(可以通过定义非const指针指向该对象,从而修改其|Q?Q?font color=#ff0000>const指针:</font> type <font color=#ff00ff>*const v</font>;可以修改指针指向的对象,但不能修Ҏ(gu)针本w。(3Q?font color=#ff0000>指向const对象的const指针</font>Q?font color=#ff00ff>const type *const v;</font> Q?font color=#404040>备注Q?font color=#ff00ff>typedef string * name;  const name v;</font> //</font><font color=#404040>v的类型是指向stringcd的const指针。)</font>  <p>4.<font color=#ff0000>const与一般函敎ͼ</font> (1)<font color=#ff00ff> const type1 func(type2 v);</font> 函数q回gؓconst; (2) <font color=#ff00ff>type1 func(const type2 v);</font>形参为const变量。因为Ş参不是引用,不修改实参的|所以此时的const不v特别的作用;Q?Q?font color=#ff00ff>type1 func(const type& v);</font> 形参为const引用Q不修改传递到形参的实参倹{?指针时相同,不修Ҏ(gu)针指向的对象的倹{? <p>5.<font color=#ff0000>const与类成员函数Q常量成员函敎ͼ</font>Qtype func(type v) const;{h(hun)于type func(const *this,type v) const; 它是值this指针是指向const对象的指针,q个函数不改变调用该函数的对?font color=#404040>。(备注Q事实上是不可以昄使用this指针作ؓ形参的,但可以在函数体中昄C用this指针。)</font> <p>6.<font color=#ff0000>const与P代器Q?/font><font color=#ff00ff>vector<type>::const_iterator it;  const vector<type>::iterator iter;</font> it指向的元素不能修改,iter指向的元素可以修改,但P代器本n不能修改? <p>7.<font color=#ff0000>const与容器:</font> <font color=#ff00ff>const vector<type> vec;</font> 需要注意此时定义的容器q代器必Lconst_iterator型?img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9aconst%e9%99%90%e5%88%b6%e7%ac%a6&referrer=" width=1 border=0><img style="POSITION: absolute" height=0px alt="" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com&GT1=liyuxia-life" width=0px><br>文章来源:<a >http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!243.entry</a> </p> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/79759.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2009-02-28 21:38 <a href="http://www.shnenglu.com/liyuxia713/archive/2009/02/28/79759.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[导入]函数参数http://www.shnenglu.com/liyuxia713/archive/2009/02/28/79760.htmlq运?/dc:creator>q运?/author>Sat, 28 Feb 2009 11:18:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2009/02/28/79760.htmlhttp://www.shnenglu.com/liyuxia713/comments/79760.htmlhttp://www.shnenglu.com/liyuxia713/archive/2009/02/28/79760.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/79760.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/79760.html1.非引用Ş?/font>通过复制实参值创建和定义函数的局部对象,从而对形参的操作不改变实参倹{因为是复制初始化在形参前加const无媄响?font color=#ff00ff>type function(type2 v) ?font color=#ff00ff>type function(const type2 v)是等L。这一点在函数重蝲时也要注意,如果同时出现上述的两个函数则是重复定义而不是重载?/font>

2.引用形参是实参的别名Q从而对形参的操作改变实参倹{用途:W一Q大型的参数通过复制初始化效率低时用引用参数Q第二,对于一些不能复制初始化的参敎ͼW三Q可以通过增加形参q回额外的信息?注意Q?/font>type function(type2 v) ?type function(const type2 v)是不同的?/font>

3.const& :一般不需要修改实参时用const引用。这主要是考虑到非const引用形参的如下缺点:W一Q传递的实参必须与Ş参类型完全相同,而不包含可以隐式转换的类型;W二Q传递的实参不能是constQ右倹{?/font>

4.指向指针的引?/font> type* &vQ?/font>

5.Q?)非引用数lŞ?/font>Q?font color=#ff00ff>int*, int[],int[n]?/font>q三个是{h(hun)的,都传递指向第一个元素的指针。这样容易生越界。如何防止越界呢Q第一Q通过l束标记数l的l束Q如C风格字符ԌW二Q用标准库规范,传递第一个和最后一个的下一个元素的指针做参敎ͼW三Q显CZ递数l大的形参?/font>

5.Q?Q?font color=#ff0000>引用数组形参Q?font color=#ff00ff>type (&arr)[n]Q?/font> 注意两点Q一是,圆括h必须的,因ؓ下标q算W的优先U更高;二是Q表C数l元素个数的n是必ȝQ因为引用是数组别名Q而数l是固定长度的?/font>

6.默认实参Q第一要考虑位置Q第二,如果提供实参Q则它覆盖默认的实参倹{?/font>
文章来源:http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!242.entry



]]>
[导入]C风格字符串与stringcdhttp://www.shnenglu.com/liyuxia713/archive/2009/02/27/79761.htmlq运?/dc:creator>q运?/author>Fri, 27 Feb 2009 13:18:00 GMThttp://www.shnenglu.com/liyuxia713/archive/2009/02/27/79761.htmlhttp://www.shnenglu.com/liyuxia713/comments/79761.htmlhttp://www.shnenglu.com/liyuxia713/archive/2009/02/27/79761.html#Feedback0http://www.shnenglu.com/liyuxia713/comments/commentRss/79761.htmlhttp://www.shnenglu.com/liyuxia713/services/trackbacks/79761.htmlC风格字符串等价于string cd字符串字面?

string str1("Hello!"); //ok.

char *str2 = str1; //error.

char *str3 = str2.c_str(); //ok. but not quite.

//注意c_str()q回的数据类型是const char

const char *str4 = str2.c_str(); //ok.
文章来源:http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!232.entry



]]>
[导入]动态内存管?/title><link>http://www.shnenglu.com/liyuxia713/archive/2009/02/27/79762.html</link><dc:creator>q运?/dc:creator><author>q运?/author><pubDate>Fri, 27 Feb 2009 13:09:00 GMT</pubDate><guid>http://www.shnenglu.com/liyuxia713/archive/2009/02/27/79762.html</guid><wfw:comment>http://www.shnenglu.com/liyuxia713/comments/79762.html</wfw:comment><comments>http://www.shnenglu.com/liyuxia713/archive/2009/02/27/79762.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/liyuxia713/comments/commentRss/79762.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/liyuxia713/services/trackbacks/79762.html</trackback:ping><description><![CDATA[<p><u><font color=#800080>动态分配的数组Q?/font></u>成员为类cd时用默认构造函敎ͼ为内|类型时不自动初始化。(同函数内部变量的自动初始化) <p>可以采用 <font color=#ff00ff>new type[]()</font> 由内|类型的默认值初始化Q注意不能在圆括号内写入值初始化? <p>但是当是单个对象定义时可以: <font color=#ff00ff>new type(value)</font> 是有效的<img height=1 alt="" src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=982263437555584821&page=RSS%ef%bc%9a%e5%8a%a8%e6%80%81%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86&referrer=" width=1 border=0><img style="POSITION: absolute" height=0px alt="" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=81873&RF=&DI=3919&PS=85545&TP=liyuxia-life.spaces.live.com&GT1=liyuxia-life" width=0px><br>文章来源:<a >http://liyuxia-life.spaces.live.com/Blog/cns!DA1B364675ACF35!231.entry</a> </p> <img src ="http://www.shnenglu.com/liyuxia713/aggbug/79762.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/liyuxia713/" target="_blank">q运?/a> 2009-02-27 21:09 <a href="http://www.shnenglu.com/liyuxia713/archive/2009/02/27/79762.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.shnenglu.com/" title="精品视频久久久久">精品视频久久久久</a> <div class="friend-links"> </div> </div> </footer> <a href="http://www.9dqmu.cn" target="_blank">þۺƵ</a>| <a href="http://www.js157.cn" target="_blank">ۺ˾þôý</a>| <a href="http://www.gfwi.cn" target="_blank">Ļþ</a>| <a href="http://www.xggppz8.cn" target="_blank">þþƷƷ޾Ʒ </a>| <a href="http://www.jxjkyt.cn" target="_blank">ھƷþþþӰԺվ </a>| <a href="http://www.ggjkb.cn" target="_blank">99þ99þ</a>| <a href="http://www.lifeindex.cn" target="_blank">69Ʒþþþ9999APGF </a>| <a href="http://www.tengfangwang.cn" target="_blank">һþ</a>| <a href="http://www.lueyi.com.cn" target="_blank">ҹƷƬþ</a>| <a href="http://www.hthotel.com.cn" target="_blank">þɫۺҹž</a>| <a href="http://www.pvzj.cn" target="_blank">þþƷavˮ </a>| <a href="http://www.wenydz.cn" target="_blank">Ůþþþþjþ</a>| <a href="http://www.xygree.cn" target="_blank">þþþþ</a>| <a href="http://www.salhm.cn" target="_blank">99þþþþѿ</a>| <a href="http://www.shssdq.cn" target="_blank">þƵ</a>| <a href="http://www.up2me.cn" target="_blank">þþۺϾɫۺ̾ </a>| <a href="http://www.fc27.cn" target="_blank">þþþþþž99Ʒ</a>| <a href="http://www.whruide.cn" target="_blank">ƷþþþþĻ</a>| <a href="http://www.idigest.com.cn" target="_blank">þĻƷ</a>| <a href="http://www.jzbbbs.cn" target="_blank">91þۺ</a>| <a href="http://www.alilinfen.cn" target="_blank">޾þþһ</a>| <a href="http://www.xitie520.cn" target="_blank">ƷþþĻ</a>| <a href="http://www.shensizxw.cn" target="_blank">2021þþƷѹۿ</a>| <a href="http://www.oubaidu.cn" target="_blank">Ʒþþ21p</a>| <a href="http://www.androidfans.com.cn" target="_blank">þĻһ</a>| <a href="http://www.rct7.cn" target="_blank">LƷþ</a>| <a href="http://www.520chuanqi.cn" target="_blank">þþþĻɫ </a>| <a href="http://www.busher.cn" target="_blank">ŷþþþƷ</a>| <a href="http://www.zjfinancial.cn" target="_blank">þþƷһ</a>| <a href="http://www.yueyuju.cn" target="_blank">˾þվ</a>| <a href="http://www.gcjszzbjb.cn" target="_blank">þþƷ</a>| <a href="http://www.gbestech.cn" target="_blank">Ʒþþþþ³</a>| <a href="http://www.fjprxr.cn" target="_blank">þ¾Ʒ</a>| <a href="http://www.iningyu.cn" target="_blank">޾þһ </a>| <a href="http://www.liuxuehanguo.cn" target="_blank">þ99Ʒþ99ý</a>| <a href="http://www.klhome.com.cn" target="_blank">ŷսþþþþþ</a>| <a href="http://www.80008000.cn" target="_blank">ձƷþþþĻ</a>| <a href="http://www.ltak.cn" target="_blank">þþƷɧ</a>| <a href="http://www.fimtb.cn" target="_blank">99ƷѾþþþþ</a>| <a href="http://www.printinginfo.com.cn" target="_blank">18պҹþó</a>| <a href="http://www.bxzpzlb.cn" target="_blank">þƵ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>