Forward Declaration of a Base Class 什么时候该用

本文探讨了C++中前向声明的应用场景及其限制条件。指出前向声明不能用于成员属性声明、对象定义与删除等操作,但可用于函数参数、成员指针等声明。文章强调合理使用前向声明可以有效减少头文件依赖。

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

You can not use forward declarations in any of those cases as the compiler needs to know the size of the class.

You can only use forward declaration in a set of operations:

  • declaring functions that take the forward declared class as parameters or returns it
  • declaring member pointers or references to the forward declared class
  • declaring static variables of the forward declared type in the class definition

You cannot use it to

  • declare a member attribute of the given type (compiler requires size)
  • define or create an object of the type or delete it
  • call any static or member method of the class or access any member or static attribute
At the end, the advice that was given to you by Roddy: forward declare as much as you can, but assume that some things must be included.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值