harttle.github.io icon indicating copy to clipboard operation
harttle.github.io copied to clipboard

2015/07/05/cpp-pointers-and-references

Open utterances-bot opened this issue 7 years ago • 4 comments

C++手稿:指针与引用 | Harttle Land

C++的引用和指针始终是最容易出错的地方,大量的C++错误都是由空引用和空指针造成的。 与此同时,常量指针、函数指针、数组指针也是容易产生困惑的地方。 本文便来总结一下C++中引用和指针的用法,以及智能指针的概念。

https://harttle.land/2015/07/05/cpp-pointers-and-references.html

utterances-bot avatar May 22 '18 16:05 utterances-bot

传引用还是传值,对于调用者来讲是没有区别的。 这句话觉得有点问题,传值是拷贝传递,不影响外部,传引用会改变原来的值不是吗?

ghost avatar May 22 '18 16:05 ghost

class Array{
    int[N] array;
public:
    int& operator[](i){
        return array[i];
    }
};
Array arr;
arr[2] = 2;

这一段里面,int[N] array和 int& operator是什么意思哇?

ghost avatar May 22 '18 16:05 ghost

哦,想起来了,int&是按引用传递返回值,operator是操作符重载。

ghost avatar May 22 '18 16:05 ghost

auto_ptr好像被cpp11不建议使用。书上没说。

ghost avatar May 22 '18 16:05 ghost