C++标准,是否允许两个互不可见的类重名?

lnyat lnyat 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
以下是一个测试程序,编译器是 VS2010

test1.cpp:


class Cs
{
public:
int test()
{
return 1;
}
int test1()
{
return 10;
}
};

int test1()
{
return Cs().test() + Cs().test1();
}


test2.cpp:


class Cs
{
public:
int test()
{
return 2;
}
int test2()
{
return 20;
}
};

int test2()
{
return Cs().test() + Cs().test2();
}


main.cpp:


#include <stdio.h>
#include <stdlib.h>

int test1();
int test2();

int main()
{
printf("%d %d\n", test1(), test2());
system("pause");
return 0;
}


输出结果竟然是的:11 21


疑问:

1、同一个编译模块中,C++标准是否允许两个类重名,或者这个根本不是标准的一部分?
2、出现这样的情况是不是MS链接器的BUG?
3、如果C++标准允许类重名,链接器也没有问题,那这算不算是C++标准的一个缺陷?
...全文
评论 51
收藏 已收藏
分享
返回顶部
lnyat
C++ 语言 65,214 成员 250,514 内容