-1

I have two very related questions regarding forward declarations, their advantages and difference with #includes. 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;

  • 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)

    namespace Eigen
    {
    
    class Vector3d;
    }
    

at the top of the .h after all the other #includes or if I should use the proper header.

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. This is the only instance where I'm using a 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.

aPonza
  • 492
  • 4
  • 10

1 Answers1

1

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.

But in order to create an object of that class, even one with a default value, you're going to need its definition.

Tim Randall
  • 4,040
  • 1
  • 17
  • 39
  • Thanks for the quick reply. So is it that ConstPtr is not a pointer per se, and that's why I need to include that header as well? – aPonza Oct 17 '18 at 16:12
  • I think you will need to include the header in your CPP file if you want to call a function like `foo` that has a default parameter, because to pass the default value you need to know how to construct the object. But I did not parse the file you linked, so I'm not sure exactly what your use case is. – Tim Randall Oct 17 '18 at 18:38
  • No, ok, I got the part regarding `foo` when you said in the answer I need to create an object, which means I need the constructor and thus the include. The second bit was about the first question, which refers to ConstPtr. In the link, you would find: `typedef boost::shared_ptr< ::std_msgs::Float64MultiArray_ const> ConstPtr;` which I'm almost sure doesn't really count as a pointer since the syntax isn't something like `type * ConstPtr`. – aPonza Oct 19 '18 at 07:38
  • Found a [related question](https://stackoverflow.com/questions/16588075/forward-declarations-and-shared-ptr) which explains I should be able to do it, however my real msg would have me include std::allocator, which I'm not really using, so I'll include the message itself since it is what I really use. Leaving the link for anybody else interested. – aPonza Oct 19 '18 at 07:52