新建一个dll工程:TestClassDemo,然后添加一个TestClassDemo.h,TestClassDemo.cpp默认存在。
TestClassDemo.h:
#pragma once
class IAnimal
{
public:
virtual void run() = 0;
virtual void sleep() = 0;
virtual void releaseObj() = 0;
};
__declspec(dllexport) IAnimal*GetAnimal();
TestClassDemo.h:
#include "stdafx.h"
#include <iostream>
#include "TestClassDemo.h"
class Cat :public IAnimal
{
public:
Cat()
{
std::cout << "猫在创建\n";
}
~Cat()
{
std::cout << "猫在销毁\n";
}
void run()
{
std::cout << "猫在跑\n";
}
void sleep()
{
std::cout << "猫在睡觉\n";
}
void releaseObj()
{
delete this;
}
};
__declspec(dllexport) IAnimal*GetAnimal()
{
return new Cat();
}
在另外的工程中使用:
#include <iostream>
using namespace std;
#include "D:\\vs2015\\Code\\TestDemo\\TestClassDemo\\TestClassDemo.h"
#pragma comment(lib,"D:\\vs2015\\Code\\TestDemo\\Debug\\TestClassDemo.lib")
void main()
{
IAnimal *pAnimal = GetAnimal();
pAnimal->run();
pAnimal->sleep();
pAnimal->releaseObj();
system("pause");
}
结果: