现在的位置: 首页 > 综合 > 正文

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

2014年08月28日 ⁄ 综合 ⁄ 共 332字 ⁄ 字号 评论关闭

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);

抱歉!评论已关闭.