联系
Knight's Tale » 技术

Name of "Struct" and "function" can be the same

2011-01-06 14:11

when  I read the code ( From book "Beginning Linux Programming 4th Edition" )

int stat(const char path, struct stat buf);
I found that this function name "stat" is equal to the struct name "stat". First, I feel a little surprised. How the struct name can be equal to the name of function? Why the G++(GCC) compiler doesn't throw out complains?

Then I tried the following codes which are written by me:

struct statExample
{

};

void statExample(struct statExample *buf=NULL) { printf("Nothing\n"); }

void main() { struct statExample x; statExample(&x); }

There is nothing error (compile & link ) in the above codes. So it is proved that the name of "Struct" and "function" can be the same.

It's noticed that class name should not be the same to the function because class could have construct function. Variable name also should not be equal to the function name.