pre_declaration

本文探讨了在C++编程中,如何通过前置声明替代头文件包含,减少编译依赖,提高编译效率。详细解释了在哪些场景下可以使用前置声明,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前置声明

​ 头文件A包含另一个头文件B,是为了引入在头文件A中使用到的类,函数,结构体,枚举或者其他实体声明。在

面向对象程序中,最常见的情况是头文件A想要引入头文件B中的一个或者多个类的声明。不过,很多情况下头文件A实际上不必包含头文件B,只需要提供前置声明即可。前置声明可以在下列几种情况下使用:

  • 不需要知道类的大小,如果包含的类作为成员变量,或者打算派生当前包含类,那么就需要知道类大小
  • 没有引用类的任何成员方法
  • 没有引用类的任何成员变量
class B;//前置声明
class A
{
public:
	void SetObject(const B& obj);
private:
	B* mobj;
};

​ 如果要修改类A的定义使得编译器知道类B的实际大小,那么就需要包含类B的实际声明,也就得包含B的头文件

#include "B.h"
class A
{
public:
	void SetObject(const B& obj);
private:
	B mobj;
};

值得注意的是,如果以传值方法传递变量参数,或者以传值方式返回值,那么前置声明也可满足要求。也许你会认为编译器此时需要知道类的大小,但实际上只有实现方法的代码和任何调用它的客户代码需要知道类的大小.

class B;
class A
{
public:
	void SetObject(B obj);
	B GetObject() const;
};

建议:STL头文件直接include即可,一般来说前置声明的都是自己定义的API接口

void DispatchCtrl::setStreamZoomRatio(struct zoom_info zoomRatio, bool prev_izm) { 949 if (mState == sStreamOn) { 950 std::lock_guard<std::mutex> l(mZoomLock); CID 206271:(#7 of 15):Struct Type Not Allowed In Parameterlist (ILLEGAL_PARAM) [ "选择问题" ] 951 setPsParam(mCameraId, PREVIEW_TYPE,0, OPLUS_APP_ZOOMRATIO, &zoomRatio); 952 if (mPrevIzmOn) { CID 206271:(#1 of 15):Struct Type Not Allowed In Parameterlist (ILLEGAL_PARAM) [ "选择问题" ] CID 206271:(#3 of 15):Struct Type Not Allowed In Parameterlist (ILLEGAL_PARAM) [ "选择问题" ] CID 206271:(#4 of 15):Struct Type Not Allowed In Parameterlist (ILLEGAL_PARAM) [ "选择问题" ] 953 zoomRatio.zoom_ratio = zoomRatio.zoom_ratio >= 3.0 ? zoomRatio.zoom_ratio / 3.0 : 1; CID 206271:(#2 of 15):Struct Type Not Allowed In Parameterlist (ILLEGAL_PARAM) [ "选择问题" ] CID 206271:(#5 of 15):Struct Type Not Allowed In Parameterlist (ILLEGAL_PARAM) [ "选择问题" ] CID 206271:(#8 of 15):Struct Type Not Allowed In Parameterlist (ILLEGAL_PARAM) [ "选择问题" ] 954 zoomRatio.pre_zoom = zoomRatio.pre_zoom >= 3.0 ? zoomRatio.pre_zoom / 3.0 : 1; CID 206271:(#6 of 15):Struct Type Not Allowed In Parameterlist (ILLEGAL_PARAM) [ "选择问题" ] 955 prevIzmCbZoomInfo(zoomRatio.zoom_ratio); 956 } 957 958 int oplusfeaturetype = 0; 959 int capture_mode =0; 960 uint8_t fast_shout = 0; 961 getPsParam(mCameraId, SNAPSHOT_TYPE, 0, OPLUS_APS_FEATURETYPE, &oplusfeaturetype); 962 getPsParam(mCameraId, SNAPSHOT_TYPE, 0,OPLUS_CAPTURE_TYPE,&capture_mode); 963 getPsParam(mCameraId, SNAPSHOT_TYPE, 0, OPLUS_FAST_SHOT_ENABLE, &fast_shout); 964 CID 206271: (#15 of 15): Struct Type Not Allowed In Parameterlist (ILLEGAL_PARAM) 1. unsafe_reference: The reference of a struct member of parameter is unsafe:zoomRatio 965 LOGD("oplusfeaturetype:%d capture_mode:%d fast_shout: %d zoom_ratio:%f pre_zoom:%f",oplusfeaturetype,capture_mode,fast_shout,zoomRatio.zoom_ratio,zoomRatio.pre_zoom); 966 for (auto dstream:mDStreamVec) {
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值