可以说我有两个文件:
/**
* class.cpp
*/
#include <stdio.h>
class foo
{
private:
int func();
};
int foo::func(void)
{
printf("[%s:%d]: %s\n", __FILE__, __LINE__, __FUNCTION__);
return -1;
}
Run Code Online (Sandbox Code Playgroud)
和
/**
* main.cpp
*/
#include <stdio.h>
namespace foo
{
int func(void);
}
int main(void)
{
int ret = foo::func();
printf("[%s:%d]: ret=%d\n", __FILE__, __LINE__, ret);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
汇编如下:
g++ -o a.out main.cpp class.cpp
Run Code Online (Sandbox Code Playgroud)
可执行文件有一个输出:
[class.cpp:15]: func
[main.cpp:14]: ret=-1
Run Code Online (Sandbox Code Playgroud)
最后我的问题是:
为什么这个示例代码编译没有任何错误,我们能够调用类foo的私有方法?
用gcc 4.6.3编译但不仅如此.我知道,编译器并不区分这两个符号(FUNC从空间中的函数foo的和私有函数FOO从类Foo).nm的 …