C++引用
#include "stdio.h"
#include "my.h"
void Sqrt(int &num)
{
num*=num;
}
void main()
{
int count=5;
const int *p=&count;
const int *&r=p;
//*r=123; 这一句编译器会报错
printf("%d\n",count);
}
void*& Fun()
{
int t=123;
int *p=&t;
int *&r=p;
return (void*&)r;
#include "my.h"
void Sqrt(int &num)
{
num*=num;
}
void main()
{
int count=5;
const int *p=&count;
const int *&r=p;
//*r=123; 这一句编译器会报错
printf("%d\n",count);
}
void*& Fun()
{
int t=123;
int *p=&t;
int *&r=p;
return (void*&)r;
}
改变书上的例子:
// strtref.cpp -- using structure references
#include <iostream>
using namespace std;
struct sysop
{
char name[26];
char *quote;
int used;
};
const sysop & use(sysop & sysopref); // function with a reference return type
char* Fun()
{
cout<<"正在执行";
return "Fun";
}
int main()
{
sysop looper =
{
"Rick \"Fortran\" Looper",
"I'm a goto kind of guy.",
0
};
sysop ss={"ak","bb",456};
looper=ss;
printf("%d,%c\n",looper.used,*(looper.name+1));
return 0;
}
const sysop & use(sysop & sysopref)
{
cout << sysopref.name << " says:\n";
cout << sysopref.quote << endl;
sysopref.used++;
return sysopref;
}