1. 值传递(拷贝)、引用传递
int a = 1, b = 2;
swap(&a, &b); --> void swap(int *a, int *b);
//a = 2, b= 1;
int *p = NULL;
test(p); --> void test(int *p);
//p = NULL;
int a = 1;
test(a); --> void test(int &a) { a = 2}
//a = 2;
int *p = NULL;
test(p); --> void test(int *&p) { int a = 1; p = &a;}
//p不为NULL
//引用进行参数传递时事实上传递的是参数本身,而不是拷贝。
//所以要达到同时修改指针的目的的话,得使用引用。
引用传递类似于将原对象取了一个小名,操作的对象还是原对象。
引用传递可以节省开辟副本的时间和空间,但是并不是所有情况都可以用引用传递,如下分析:
类引用等例子说明引用传递的使用
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class Myint {
private:
int x;
public:
Myint(){}
Myint(int xx) {x = xx;}
Myint