开发者

What is the proper way to forward declare a pointer to a class for use inside the class declaration?

For example,

   class Segment
   {
      frie开发者_如何学运维nd bool someFunc( P_Segment p );
   };

typedef boost::shared_ptr<Segment> P_Segment;

How best to declare P_Segment so this compiles?


In this case you have no choice since you can't forward declare typedefs. You'll have to forward declared the Segment class instead.

class Segment;

typedef boost::shared_ptr<Segment> P_Segment;

class Segment
{
    friend bool someFunc( P_Segment p );
};


Nothing wrong with what the others have said, but just for an alternative:

class Segment
{
public:
   typedef boost::shared_ptr<Segment> P_Segment;
   friend bool someFunc( P_Segment p );
};

using Segment::P_Segment;


Forward declare your class, and after that forward declare smart pointer (shared_ptr can accepts incomplete types):

   class Segment;
   typedef boost::shared_ptr<Segment> Segment_PTR;

   class Segment
   {
      friend bool someFunc(Segment_PTR p);
   };


Another way would be to make the typedef a member of the class:

class Segment
{
    typedef boost::shared_ptr<Segment> pointer;
    friend bool someFunc( pointer p );
};

This changes how you access it of course, you noe have to use Segment::pointer outside of the class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜