您的位置:首页 > 其它

初始化COM类,在FinalConstruct与构造函数中初始化有何区别

2011-05-17 14:03 260 查看
There are advantages to performing initialization in
FinalConstruct rather than the constructor of your class:

You cannot return a status code from
a constructor, but you can return an HRESULT by means
of FinalConstruct 's return value. When objects of your
class are being created using the standard class
factory provided by ATL, this return value is propagated
back to the COM client allowing you to provide them
with detailed error information.

You
cannot call virtual functions through the virtual function
mechanism from the constructor of a class. Calling a
virtual function from the constructor of a class
results in a statically resolved call to the function
as it is defined at that point in the inheritance
hierarchy. Calls to pure virtual functions result in
linker errors.

Your class is not the
most derived class in the inheritance hierarchy — it
relies on a derived class supplied by ATL to provide
some of its functionality. There is a good chance
that your initialization will need to use the features
provided by that class (this is certainly true when
objects of your class need to aggregate other objects),
but the constructor in your class has no way to
access those features. The construction code for your
class is executed before the most derived class is
fully constructed.

However, FinalConstruct is
called immediately after the most derived class is fully
constructed allowing you to call virtual functions and
use the reference-counting implementation provided by ATL.

----From MSDN
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: