And*_*rew 12 c++ typedef class forward-declaration
我想在我的软件中使用类的前向声明,所以我可以使用typedef
并在类完全声明中使用它们.
像这样的Smth:
class myclass;
typedef boost::shared_ptr<myclass> pmyclass;
typedef std::list<pmyclass > myclasslist;
class myclass : public baseclass
{
private: // private member declarations
__fastcall myclass();
public: // public member declarations
__fastcall myclass(myclass *Parent)
: mEntry(new myclass2())
{
this->mParent = Parent;
}
const myclass *mParent;
myclasslist mChildren;
boost::scoped_ptr<myclass2> mEntry;
};
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:这种方法有什么缺点吗?我记得有关前向声明的析构函数问题的一些讨论,但我并没有把所有东西都拿出来.
还是有其他选择来实现这样的东西?
谢谢.
编辑:我找到了我所指的讨论:这里
Coi*_*oin 22
主要缺点是一切.前向声明是节省编译时间的折衷方案,让您在对象之间具有循环依赖关系.但是,成本是您只能使用类型作为引用,并且不能对这些引用执行任何操作.这意味着,没有继承,没有将它作为值传递,没有在该类中使用任何嵌套类型或typedef等等......这些都是很大的缺点.
您正在讨论的具体销毁问题是,如果您仅转发声明类型并且恰好仅在模块中删除它,则行为未定义且不会引发错误.
例如:
class A;
struct C
{
F(A* a)
{
delete a; // OUCH!
}
}
Run Code Online (Sandbox Code Playgroud)
Microsoft C++ 2008不会调用任何析构函数并抛出以下警告:
warning C4150: deletion of pointer to incomplete type 'A'; no destructor called
: see declaration of 'A'
Run Code Online (Sandbox Code Playgroud)
因此,您必须保持警惕,如果您将警告视为错误,这不应该是一个问题.