C++类静态对象的问题

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

#include <iostream>
using namespace std;

class Base
{
public:
Base()
{
m_p = this;
}
virtual void func() { m_p->print(); }

virtual void print() { cout << "Base" << endl; }

static Base* m_p;
};

Base * Base::m_p = NULL;

class Derive : public Base
{
public:
void print() { cout << "Derive" << endl; }
};

int main()
{
Base * pDerive = new Derive;
Base * pBase = new Base;
pBase->func();
pDerive->func();
return 0;
}

打印
Base
Base
如果
int main()
{
Base * pBase = new Base;
Base * pDerive = new Derive;
pBase->func();
pDerive->func();
return 0;
}
这样写打印
Derive
Derive
为什么会结果不一样
...全文
评论 3
收藏 已收藏
分享
返回顶部
阿尔博特
C++ 语言 65,214 成员 250,514 内容