转发声明指向类的指针以在类声明中使用的正确方法是什么?
例如,
class Segment
{
friend bool someFunc( P_Segment p );
};
typedef boost::shared_ptr<Segment> P_Segment;
如何最好地声明 P_Segment 以便编译?
For example,
class Segment
{
friend bool someFunc( P_Segment p );
};
typedef boost::shared_ptr<Segment> P_Segment;
How best to declare P_Segment so this compiles?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这种情况下,您别无选择,因为您无法转发声明 typedef。您必须转发声明的
Segment
类。In this case you have no choice since you can't forward declare typedefs. You'll have to forward declared the
Segment
class instead.其他人说的没有错,只是提供一种替代方案:
Nothing wrong with what the others have said, but just for an alternative:
向前声明您的类,然后向前声明智能指针(shared_ptr 可以接受不完整类型):
Forward declare your class, and after that forward declare smart pointer (shared_ptr can accepts incomplete types):
另一种方法是使 typedef 成为类的成员:
当然,这会改变您访问它的方式,您不必在类之外使用
Segment::pointer
。Another way would be to make the typedef a member of the class:
This changes how you access it of course, you noe have to use
Segment::pointer
outside of the class.