新建一个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#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(); }
在另外的工程中使用:
#includeusing 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"); }
结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)