转发声明和shared_ptr [英] Forward declarations and shared_ptr
问题描述
我正在尝试重构代码,以便使用前向声明而不是包含许多标头.我对此并不陌生,并且对boost :: shared_ptr有疑问.
I'm trying to refactor my code so that I use forward declarations instead of including lots of headers. I'm new to this and have a question regarding boost::shared_ptr.
说我有以下界面:
#ifndef I_STARTER_H_
#define I_STARTER_H_
#include <boost/shared_ptr.hpp>
class IStarter
{
public:
virtual ~IStarter() {};
virtual operator()() = 0;
};
typedef boost::shared_ptr<IStarter> IStarterPtr;
#endif
然后我在另一个类中有一个函数,该函数将IStarterPtr对象作为参数,例如:
I then have a function in another class which takes an IStarterPtr object as argument, say:
virtual void addStarter(IStarterPtr starter)
{
_starter = starter;
}
...
IStarterPtr _starter;
如何在不包含IStarter.h的情况下转发IStarterPtr声明?
how do I forward declare IStarterPtr without including IStarter.h?
如果相关,我正在使用C ++ 98.
I'm using C++98 if that is of relevance.
推荐答案
只要不对它们调用*或->,共享指针就可以与正向声明的类型一起使用,因此应该可以简单地编写:-
Shared pointers work with forward declared types as long as you dont call * or -> on them so it should work to simply write :-
class IStarter;
typedef boost::shared_ptr<IStarter> IStarterPtr;
您当然需要包含< boost/shared_ptr.hpp>
这篇关于转发声明和shared_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!