我可以在 DLL 中放置一个类吗? 我写的课是这样的:
class SDLConsole
{
public:
SDLConsole();
~SDLConsole(){};
void getInfo(int,int);
void initConsole(char*, char*, SDL_Surface*, int, int, int);
void sendMsg(char*,int, SDL_Surface*);
void cls(SDL_Surface*);
private:
TTF_Font *font;
SDL_Surface *consoleImg;
int width, pos, height, line, size, ctLine;
SDL_Surface* render(char*,int);
};
我知道如何加载 DLL 并在 DLL 中使用函数,但是如何将类放入 DLL 中?非常感谢。
最佳答案
如果您使用运行时动态链接(使用 LoadLibrary 加载 dll)您无法直接访问该类,您需要为您的类声明一个接口(interface)并创建一个返回此类实例的函数,如下所示:
class ISDLConsole
{
public:
virtual void getInfo(int,int) = 0;
virtual void initConsole(char*, char*, SDL_Surface*, int, int, int) = 0;
virtual void sendMsg(char*,int, SDL_Surface*) = 0;
virtual void cls(SDL_Surface*) = 0;
};
class SDLConsole: public ISDLConsole
{
//rest of the code
};
__declspec(dllexport) ISDLConsole *Create()
{
return new SDLConsole();
}
否则,如果您在加载时链接dll,只需使用icecrime提供的信息:http://msdn.microsoft.com/en-us/library/a90k134d.aspx
关于c++ - 如何在 DLL 中使用类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4555961/