C++类静态对象的问题
#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
为什么会结果不一样
...全文
相关推荐
类的静态数据成员-用来统计对象存在的个数
类的静态数据成员的实际应用场景之一:用来统计该类对象存在的个数;并且如果在头文件中初始化静态变量将导致obj重复定义的错误,解决办法:将静态成员的初始化放到.cpp文件中
静态对象.cpp
翁恺教授视频《C++面向对象程序设计》源码
Cpp_practice
cpp_practice Stephen Prata,Maeng Chul-Gyu翻译C ++ Basics Plus 6th Edition示例代码实践库。
Cpp Primer Plus第6版源码
Cpp Primer Plus第6版源码
cpp代码-C++:静态函数、静态成员 static
cpp代码-C++:静态函数、静态成员 static


