摘要class 不能直接映射,有什么优雅的解决办法吗?
Abstract class not mapping directly, any elegant solutions?
下面的代码无法实例化 Display_OpenGL class,因为它没有考虑将 Surface_OpenGL 中的 Surface 实现映射到 Display:Surface class。
取消注释这一行可以解决问题,但这并不优雅。
//void Draw() { Surface_OpenGL::Draw(); }
我想做的事情有更好的解决方案吗?
Draw 已在 Surface_OpenGL 中定义,但似乎也需要为 Display 明确定义,有什么好的解决方案吗?
谢谢。
class Surface
{
public:
virtual void Draw() = 0;
};
class Display : public Surface
{
};
class Surface_OpenGL : public Surface
{
public:
void Draw(){}
};
class Display_OpenGL : public Display, public Surface_OpenGL
{
public:
//void Draw() { Surface_OpenGL::Draw(); }
};
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
//Error cannot instantiate abstract class
Display *display = new Display_OpenGL();
display->Draw();
delete display;
return 0;
}
这就像经典的钻石继承案。尝试:
class Surface
{
public:
virtual void Draw() = 0;
};
class Display : virtual public Surface
{
};
class Surface_OpenGL : virtual public Surface
{
public:
void Draw(){}
};
class Display_OpenGL : public Display, public Surface_OpenGL
{
public:
//void Draw() { Surface_OpenGL::Draw(); }
};
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
//Error cannot instantiate abstract class
Display *display = new Display_OpenGL();
display->Draw();
delete display;
return 0;
}
下面的代码无法实例化 Display_OpenGL class,因为它没有考虑将 Surface_OpenGL 中的 Surface 实现映射到 Display:Surface class。
取消注释这一行可以解决问题,但这并不优雅。 //void Draw() { Surface_OpenGL::Draw(); }
我想做的事情有更好的解决方案吗? Draw 已在 Surface_OpenGL 中定义,但似乎也需要为 Display 明确定义,有什么好的解决方案吗?
谢谢。
class Surface
{
public:
virtual void Draw() = 0;
};
class Display : public Surface
{
};
class Surface_OpenGL : public Surface
{
public:
void Draw(){}
};
class Display_OpenGL : public Display, public Surface_OpenGL
{
public:
//void Draw() { Surface_OpenGL::Draw(); }
};
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
//Error cannot instantiate abstract class
Display *display = new Display_OpenGL();
display->Draw();
delete display;
return 0;
}
这就像经典的钻石继承案。尝试:
class Surface
{
public:
virtual void Draw() = 0;
};
class Display : virtual public Surface
{
};
class Surface_OpenGL : virtual public Surface
{
public:
void Draw(){}
};
class Display_OpenGL : public Display, public Surface_OpenGL
{
public:
//void Draw() { Surface_OpenGL::Draw(); }
};
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
//Error cannot instantiate abstract class
Display *display = new Display_OpenGL();
display->Draw();
delete display;
return 0;
}