‘bout_"const char*& xx"
2012-02-16 16:35
525 查看
func(const char*& cp)
cp是指向“const char”的指针。cp他是引用。
----------------------------------------------------------------------------
const char* cp
cp是指向“const char”的指针。当然不是说cp必须只能指向const char,他也能指向char类型。
但意思是,cp可以修改自己的值从而指向不同地址,但是不能修改指向地址的存储的值。
即使是指向char类型的。
当你
char c = 'a'; const char* cp = &c; *cp = ......;
上面编译就通不过。
------------------------------------------------------------------------------
下面是个比较全面的对比例子。
------------------------------------------------------------------------------
#include <iostream> using namespace std; char x = 'b'; void func(const char*& cp) { cp = &x; } void funcEX(const char* cp) { cp = &x; } //this function cannot compile successfully. /* void funcEX1(const char*& cp) { *cp = x; //try to modify the value which cp points to.failure! }*/ void main() { char b = 'a'; // //const char b = 'a'; //is ok const char* c = &b; const char*& p = c; cout<<"p:address:"<<(int)&p<<";"<<(int)p<<";value:"<<*p<<endl; cout<<"c:address:"<<(int)&c<<";"<<(int)c<<";value:"<<*c<<endl; cout<<"b:address:"<<(int)&b<<";value:"<<b<<endl; cout<<"----------------------------------------------"<<endl; funcEX(p); cout<<"p:address:"<<(int)&p<<";"<<(int)p<<";value:"<<*p<<endl; cout<<"c:address:"<<(int)&c<<";"<<(int)c<<";value:"<<*c<<endl; cout<<"b:address:"<<(int)&b<<";value:"<<b<<endl; cout<<"----------------------------------------------"<<endl; func(p); cout<<"p:address:"<<(int)&p<<";"<<(int)p<<";value:"<<*p<<endl; cout<<"c:address:"<<(int)&c<<";"<<(int)c<<";value:"<<*c<<endl; cout<<"b:address:"<<(int)&b<<";value:"<<b<<endl; cout<<"----------------------------------------------"<<endl; system("pause"); }
------------------------------------------------------------------
运行结果:
----------------------------------------------------------------
结果说明了引用的作用(funcEX()反向证明了),还有所谓的“改变自己指向的地址,而不能改变指向的地址存储的值”(注释里,funcEX1())。
相关文章推荐
- IntelliSense: "const char *" 类型的实参与 "LPCTSTR" 类型的形参不兼容 (vs2010)
- IntelliSense: "const char *" 类型的实参与 "LPCTSTR" 类型的形参不兼容 (vs2012)
- IntelliSense: "const char *" 类型的实参与 "LPCTSTR" 类型的形参不兼容
- 指针与字符串:(const)char *p2 = "lmj";定义的是一个字符串常量!符串常量,正因为是常量,所以它内部的字符是不允许修改的。
- error C2664:char *strcpy(char *,const char *)& 无法将参数 2 从CString"转换为const char *
- cvLoadImage 不能将参数1从 "_TCHAR*" 转换为"const char *"
- 不能将"constchar*"类型的值分配到"LPCWSTR"类型的实体错误解决方法
- vs2013 IntelliSense: "const char *" 类型的实參与 "LPCWSTR" 类型的形參不兼容
- const char * a = "SSS" 与 const char a[] = "SSS" 的区别之一
- 无法解析的外部符号 "wchar_t * __stdcall _com_util::ConvertStringToBSTR(char const *)"的解决办法
- "const char *" 类型的实参与 "LPCTSTR" 类型的形参不兼容
- vs2013 IntelliSense: "const char *" 类型的实参与 "LPCWSTR" 类型的形参不兼容
- error C2664: “int CWnd::GetWindowTextW(LPTSTR,int) const”: 不能将参数 1 从“char [10]”转"相关的问题
- "const char*"类型的实参与LPCTSTR类型的形参不兼容
- error C2664: “atoi”: 不能将参数 1 从“CString”转换为“const char *"
- "typedef char * pc; const pc a;“ 为什么是a而不是a所指向的字符为const
- "const char *p" 和 "char * const p" 有何区别?
- 不能将 "const char *" 类型的值分配到 "LPCWSTR" 类型的实体 错误解决方法
- "std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::str() const"问题的解决办法
- amule: FTBFS ("cannot convert 'UpnpString* {aka s_UpnpString*}' to 'const char*'")