指针引用、常量引用

来源:互联网 发布:如何成为一个网络写手 编辑:程序博客网 时间:2024/06/05 21:54

指针应用

struct Teacher
{
int age;
};
void test()
{
Teacher* teacher = NULL;
teacher = (Teacher*)malloc(sizeof(Teacher));
Teacher*& ref = teacher;
ref->age = 30;
std::cout << teacher->age << std::endl;
free(ref);
}

常量引用

void test1()
{
int a = 100;
const int& ref = a;
//ref = 200;错误,不能赋值
a = 200;
std::cout << a << std::endl;
}

0 0
原创粉丝点击