出道题给大家玩玩

deng2000 deng2000 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
闲来无聊,出道题给大家玩一玩.只博一哂,呵呵

#include <stdio.h>

class A
{
public:
int m_a;
};

class B : public A
{
public:
int m_b;
void virtual fun() {}
};

int main()
{
B b;
B* pb = &b;
A* pa = pb;
void *pb2 = (void *)pb;
void *pa2 = (void *)pa;

const char *str1 = (pa==pb) ? "yes" : "no";
const char *str2 = (pa2==pb2) ? "yes" : "no";

printf("%s %s\n", str1, str2);
return 0;
}

你能预测程序的结果吗? 是 "yes yes" 还是 "yes no", 抑或其它值?
有些牛人可能会说结果依赖于编译器, 那让我问得更具体些:
用Visual C++编译后结果如何, 在C++ Builder中又怎样呢?
...全文
评论 20
收藏 已收藏
分享
返回顶部
deng2000
C++ 语言 65,213 成员 250,513 内容