如何在c++中将此函数重写为指针

How to rewrite this function into a pointer in c++?

本文关键字:函数 重写 指针 c++      更新时间:2024-09-27
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}

在我的代码中,我所做的只是在参数和代码体中的变量ab之前添加星号。我的老师说这是错误的,所以现在我不明白了

void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

你会称你的原件为:

int one = 1;
int two = 2;
swap(one, two); // references

修改后的功能,如

swap(&one, &two); // pointers