程序 1
#include <iostream>
#include<string>
using namespace std;
void fun(const char *a)// passing address of "GeeksForGeeks" by value //
{
cout << "const fun() " << a;
}
void fun(const char *&a){// passing address of "GeeksForGeeks" by reference //
cout << "const reference fun()" <<a;
}
int main()
{
const char * ptr = "GeeksforGeeks";
fun(ptr);
return 0;
}
错误信息
In function 'int main()':
17:8: error: call of overloaded 'fun(const char*&)' is ambiguous
fun(ptr);
^
17:8: note: candidates are:
6:6: note: void fun(const char*)
void fun(const char *a)
^
11:6: note: void fun(const char*&)
void fun(const char *&a){
^
程序2
#include <iostream>
#include<string>
using namespace std;
void fun(const char *a)// passing address of "GeeksForGeeks" by value //
{
cout << "const fun() " << a;
}
void fun(const char *&a){// passing address of "GeeksForGeeks" by reference //
cout << "const reference fun()" <<a;
}
int main()
{
const char * const ptr = "GeeksforGeeks";
fun(ptr);
return 0;
}
输出
GeeksforGeeks的const fun()
const char *&p=ptr;
。但是一旦完成了这个步骤,你可以像使用原始变量一样使用引用。因此,在我们的例子中,p="hello";
的效果与ptr="hello";
相同,而p++
将会增加它所引用的指针(即指针 ptr 的内容)。 - Christophe