[英]C++ (and ROS) - Include vs. forward declare of reference with set default and typedef
I have two very related questions regarding forward declarations, their advantages and difference with #include
s.我有两个关于前向声明的非常相关的问题,它们的优点和与#include
的区别。 After reading on them it's still unclear to me if:在阅读它们之后,我仍然不清楚是否:
using a ConstPtr from a ROS message (like this ) counts as a pointer and can be (somehow) forward declared, or requires an #include
;使用来自 ROS 消息的 ConstPtr(像这样)算作一个指针,并且可以(以某种方式)前向声明,或者需要一个#include
;
void foo(const Eigen::Vector3d& scale={0.001, 0.001, 0.001});
in a .h file would be fine with something like (but this doesn't actually compile)在 .h 文件中可以使用类似的东西(但这实际上并没有编译)
namespace Eigen { class Vector3d; }
at the top of the .h after all the other #include
s or if I should use the proper header.在所有其他#include
之后的 .h 顶部,或者我是否应该使用正确的标题。
To be clear, the second issue is with the fact scale
has a default value, which is actually the one I will always be using in the .cpp.需要明确的是,第二个问题是事实scale
有一个默认值,这实际上是我将始终在 .cpp 中使用的值。 This is the only instance where I'm using a Vector3d.这是我使用 Vector3d 的唯一实例。
I'm also fairly certain if the forward declaration is enough I therefore would not need to include the proper header in the .cpp as well, since I'm only ever using the default value inside the method.我也相当确定前向声明是否足够,因此我也不需要在 .cpp 中包含正确的标头,因为我只在方法中使用默认值。
A forward declaration of X
is sufficient to use an X*
or X&
as a function parameter or class member, because the full definition of a class is not needed to be able to use its address. X
的前向声明足以将X*
或X&
用作函数参数或类成员,因为不需要类的完整定义即可使用其地址。
But in order to create an object of that class, even one with a default value, you're going to need its definition.但是为了创建该类的对象,即使是具有默认值的对象,您将需要其定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.