vc内存对齐准则
定义变量的时候尽量把相同大小的数据类型放在一起,并且按顺序声明,因为有传说中的 alignment(内存对齐)~ vc内存对齐准则
函数直接返回局部变量是不会有拷贝构造
函数直接返回局部变量是不会有拷贝构造的,下面的代码段两个地址是一样的。
1 2 3 4 5 6 7 8 9 10 11
| Test miao(){ Test t; cout<<&t<<endl; cout<<"miao"<<endl; return t; }
int main(){ Test t = miao(); cout<<&t<<endl; }
|
但是类成员函数返回类成员,是会有一次拷贝构造的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; class Test { private: public: Test(){ cout<<"construct1"<<endl; } Test(const Test &t){ cout<<"copy assignment1"<<endl; } void operator=(const Test &t){ cout<<"========1"<<endl; } ~Test(){}; };
class Test2 { private: Test t; public: Test2(){ cout<<"construct2"<<endl; } Test2(const Test2 &t){ cout<<"copy assignment2"<<endl; } void operator=(const Test2 &t){ cout<<"========2"<<endl; } Test get(){ return t; } ~Test2(){}; };
Test miao(){ Test t; cout<<&t<<endl; cout<<"miao"<<endl; return t; }
int main(){ Test2 m; Test t = m.get(); }
|
其实仔细想想还是比较合理的,因为普通的调用函数,一旦返回了,那那个栈帧就被释放了,不会有其他因素去影响其中的局部变量; 既然如此,编译器没道理要把这种注定要返回的变量放到栈顶函数的栈帧,然后释放之后再拷贝一次到返回的函数;但成员变量就不一样了,因为返回之后,他依然会收到其他因素的影响,所以只能拷贝一份回去。
git clone 太慢怎么办
将原本的网站中的 http://github.com
进行替换为 github.com.cnpmjs.org
比如将 git clone https://github.com/picoxr/gaze-3d-object.git
换成 git clone https://github.com.cnpmjs.org/picoxr/gaze-3d-object.git