Chinaunix首页 | 论坛 | 博客
  • 博客访问: 862909
  • 博文数量: 73
  • 博客积分: 7176
  • 博客等级: 少将
  • 技术积分: 2526
  • 用 户 组: 普通用户
  • 注册时间: 2004-12-14 17:16
文章分类
文章存档

2012年(1)

2011年(9)

2010年(20)

2009年(28)

2008年(15)

分类: LINUX

2011-12-22 09:47:06


当年快毕业的时候,一个同学(现在百度)来找我,说:XXX你是高手,我有个小程序不会写,你能不帮我写一下? 于是我写了这样的一段:
  1. void main(void)
  2. {

  3. int foo(int i)
  4. {
  5. return i * 2;
  6. }

  7. //do something

  8. }


用Visual C++编译不过去,我们两个一起看了半天也没看出原因来。 后来找了一个机械专业的哥们帮忙,他看了一眼,大惊失色:「你们怎么在main里定义函数?!」 计算机专业的我们都很惭愧,无地自容。



多年以后, 我在GCC的info手册里看到这样一段:

File: gcc.info, Node: Nested Functions, Next: Constructing Calls, Prev: Labels as Values, Up: C Extensions

5.4 Nested Functions
====================

A "nested function" is a function defined inside another function.
(Nested functions are not supported for GNU C++.) The nested function's
name is local to the block where it is defined. For example, here we
define a nested function named `square', and call it twice:

foo (double a, double b)
{
double square (double z) { return z * z; }

return square (a) + square (b);
}


看,我们的程序并没有错,只是Visual C++不够高级, GCC就没问题嘛! 从那以后,我无可救药的喜欢上了GCC,喜欢上了Linux……
阅读(1119) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~