笔记:函数相关注意的地方

来源:互联网 发布:起重机模拟软件 编辑:程序博客网 时间:2024/05/21 22:49

1,如果不想修改函数的参数,用const &来传,如果想改变,优先用指针而非引用。

2,不要返回局部变量的指针或者引用。(已销毁)

3,尽量用虚函数或者模版来代替函数指针。


关于函数指针:

对于函数只能做两件事:调用它或者取得它的地址。

void a(int)

{

}

void (*b)(int);

void f()

{

b = &a; // let b point to a's addr

// b =a ;     // this way is also equal to upstair

b(1); // then u could use b as the same as using a

// (*b)(1);// this way is also equal to upstair

}

经常有typedef来定义函数指针的情况:  typedef   void (*AB)(int);



原创粉丝点击