您的位置:首页 > 编程语言 > C语言/C++

高质量C/C++编程笔记.

2010-12-10 10:06 232 查看
1.若输入参数以值传递的方式传递对象,最好使用const&的方式传递.这样可以省去临时对象的构造和析构过程.提高效率.

class Test
{
public:
Test(void);
~Test(void);
void setString(const std::string& str);
protected:
std::string mStr;
};


const的意思他不仅可以接受传递的const类型参数,还可以接受非const参数
而如果没有const的话,则这个函数不能接受const类型参数。
相对于这个参数来说,并不涉及到局部变量的问题。

int test_1(const int &a)
{
00401730  push        ebp
00401731  mov         ebp,esp
00401733  push        ecx
int c=a+10;
00401734  mov         eax,dword ptr [a]
00401737  mov         ecx,dword ptr [eax]
00401739  add         ecx,0Ah
0040173C  mov         dword ptr [c],ecx
return c;
0040173F  mov         eax,dword ptr [c]
}
00401742  mov         esp,ebp
00401744  pop         ebp
00401745  ret


test_1调用处的汇编:

b=test_1(a);
0040176D  lea         eax,[a]
00401770  push        eax
00401771  call        test_1 (401005h)
00401776  add         esp,4
00401779  mov         dword ptr [b],eax


test_2执行出的汇编:

int test_2(int a)
{
00401750  push        ebp
00401751  mov         ebp,esp
00401753  push        ecx
int c=a+10;
00401754  mov         eax,dword ptr [a]
00401757  add         eax,0Ah
0040175A  mov         dword ptr [c],eax
return c;
0040175D  mov         eax,dword ptr [c]
}
00401760  mov         esp,ebp
00401762  pop         ebp
00401763  ret


test_2调用处的汇编:

b=test_2(a);
0040176D  mov         eax,dword ptr [a]
00401770  push        eax
00401771  call        test_2 (40131Bh)
00401776  add         esp,4
00401779  mov         dword ptr [b],eax

2.
char *p ="world";
p[0]='X';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: