C++ 接口与导出类

最近编写C++动态链接库模块,模块内有一些类需要被外部用户调用,这种情况下可以采用接口或者导出类实现这个功能。
如果导出类中没有任何其他的类对象作为成员,则直接导出类就可以用了,不过这种用法需要将类的头文件公开,调用者需要引用头文件才能调用导出类的功能,例如我们定义了一个点类,头文件(PointClass.h)内容

#pragma once
class _declspec(dllexport) Point
{
public:
	Point();
	Point(double x,double y);
	~Point();
	double x, y;
	double distToOrg();
};

源文件内容

#include<math.h>
#include "pointClass.h"

Point::Point()
{
	x = 0;
	y = 0;
}
Point::Point(double x, double y)
{
	this->x = x;
	this->y = y;
}
Point::~Point()
{

}
double Point::distToOrg()
{
	return pow(x * x + y * y, 0.5);
}

该类的使用,main函数如下:

#include <stdlib.h>
#include <time.h>
#include<iostream>
#include "PointClass.h"
using namespace std;
int main()
{
	//采用内部没有类对象成员的导出类
	srand((unsigned)time(0));
	double x = rand();
	double y = rand();
	Point pt(x,y);
	cout << "point coord x=" << pt.x << " y=" << pt.y <<" distToOrg="<< pt.distToOrg()<< endl;
	system("break");
}

可以编译,运行结果如下,可以看出导出类可以正常使用,其成员变量及函数均可使用。
在这里插入图片描述
也可以用接口实现上述功能,定义接口:

#pragma once
//IPoint.h
class _declspec(dllexport) IPoint
{
	virtual double getX() = 0;
	virtual double getY() = 0;
	virtual void setX(double _x) = 0;
	virtual void setY(double _y) = 0;
	virtual double distToOrg() = 0;
};

int _declspec(dllexport)_stdcall pointFactory(void** _pReturn);

重新写PointClass.h,这时不需要将其声明为导出类,而是继承了接口,使用时可以只公开接口的头文件即可

#pragma once
#include "IPoint.h"
class Point:public IPoint
{
public:
	Point();
	Point(double x,double y);
	~Point();
	double getX();
	double getY();
	void setX(double _x);
	void setY(double _y);
	double x, y;
	double distToOrg();
};

源文件内容

#include<math.h>
#include "pointClass.h"

Point::Point()
{
	x = 0;
	y = 0;
}
Point::Point(double x, double y)
{
	this->x = x;
	this->y = y;
}
Point::~Point()
{

}
double Point::getX()
{
	return x;
}
double Point::getY()
{
	return y;
}
void Point::setX(double _x)
{
	x = _x;
}
void Point::setY(double _y)
{
	y = _y;
}
double Point::distToOrg()
{
	return pow(x * x + y * y, 0.5);
}

int _declspec(dllexport)_stdcall pointFactory(void** _pReturn)
{
	Point* pPoint = new Point;
	*_pReturn = (void*)pPoint;
	if (pPoint == NULL) return 0;
	return 1;
}

pointFactory函数是为了创建一个Point对象,因为接口是虚类,不能直接生成接口对象,这里用pointFactory函数创建一个Point对象将其地址返回给接口的指针,使用接口时main函数如下:

#include <stdlib.h>
#include <time.h>
#include<iostream>
#include "IPoint.h"
using namespace std;
int main()
{
	//采用接口
	srand((unsigned)time(0));
	double x = rand();
	double y = rand();
	IPoint* pt;
	if (pointFactory((void**)&pt))
	{
		pt->setX(x);
		pt->setY(y);
		cout << "point coord x=" << pt->getX() << " y=" << pt->getY() << " distToOrg=" << pt->distToOrg() << endl;
		system("break");
	}
}

这中情况下,只需要公开接口头文件即可,编译执行效果如下:
在这里插入图片描述

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C动态库导出类的具体步骤如下所示: 1. 首先需要定义一个虚类(InterfaceClass),该类包含需要导出的函数,并且需要将该类定义前面增加API,即__declspec(dllexport)。该类的定义中推荐导出C的基础数据类型,而不是标准库或标准模板库的数据类型,以避免不同版本的库可能引发的问题。在该虚类中,使用纯虚函数的原因是为了实现接口的多态性。 2. 接着,定义实际继承自虚类的类(readImg),即我们原本需要导出的类。该类需要实现虚类中的纯虚函数。 3. 在动态库的源文件中,通过使用C语言的方式导出动态库,并提供一个函数(getInstance),该函数用于获取对应类的对象。在该函数中,通过new关键字创建实际继承类的对象,并返回指向该对象的指针。 4. 最后,在项目属性中,将配置类型设置为dll,以将项目编译为动态库。 综上所述,以上是C动态库导出类的基本步骤。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【cmake实战十】c++从动态库(dll)导出类](https://blog.csdn.net/junxuezheng/article/details/126908851)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [c++导出动态库](https://blog.csdn.net/weixin_42295969/article/details/126983694)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值