C++静态成员函数为什么可以调用通过动态绑定的虚函数?
代码如下, 导出一个纯虚类, 父类实现如下:
//CDll1.h
#pragma once
#ifdef CDLL1_EXPORTS
#define DLL1_API __declspec(dllexport)
#else
#define DLL1_API __declspec(dllimport)
#endif
class DLL1_API CDll1
{
public:
static CDll1* CreateInstance();
static void DestroyInstance(CDll1* pInstance);
virtual void Destroy() = 0;
virtual void DllTest() = 0;
};
//CDll1.cpp
#include "CDll1.h"
#include <Windows.h>
#include <tchar.h>
#include "Dll1.h" //派生类
CDll1* CDll1::CreateInstance()
{
OutputDebugString(_T("\n CDll1::CreateInstance \n"));
return new Dll1;
}
void CDll1::DestroyInstance(CDll1* pInstance)
{
OutputDebugString(_T("\n CDll1::DestroyInstance \n"));
if (pInstance != NULL)
{
pInstance->Destroy(); //这里调用通过动态绑定的虚函数
}
}
派生类实现如下:
//Dll1.h
#pragma once
#include "CDll1.h"
class Dll1 : public CDll1
{
public:
Dll1();
virtual ~Dll1();
virtual void Destroy();
virtual void DllTest();
};
//Dll1.cpp
#include "Dll1.h"
#include <windows.h>
#include <tchar.h>
Dll1::Dll1()
{
OutputDebugString(_T("\n Dll1::Dll1 \n"));
}
Dll1::~Dll1()
{
OutputDebugString(_T("\n Dll1::~Dll1 \n"));
}
void Dll1::Destroy()
{
OutputDebugString(_T("\n Dll1::Destroy \n"));
if (this != NULL)
{
delete this;
}
}
void Dll1::DllTest()
{
OutputDebugString(_T("\n Dll1::Dll1Test \n"));
}
按照之前的理解, 静态成员函数中不能引用非静态的成员, 当这里竟然可以正常通过编译, 调用结果如下:
还想请各位同志指教一二.