初始化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
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
相关文章推荐
- 初始化COM类,在FinalConstruct与构造函数中初始化有何区别
- struct初始化及构造函数与class区别
- 静态构造函数可以确定静态字段的初始化顺序及引用项目和引用该项目组件的区别
- C/C++基础问答(1):通过初始化列表和构造函数内赋值初始化成员变量的区别
- C++中构造函数初始化列表与构造函数体的区别
- C/C++ 通过初始化列表和构造函数内赋值初始化成员变量的区别
- 转载--c++中构造函数初始化的方法以及主要区别
- 视图中的构造函数、OnCreate和OnInitiaUpDate进行初始化的区别
- C++学习笔记17,构造函数体内初始化数据成员与构造函数初始化器的区别(一)
- 类的初始化列表与构造函数的区别
- 使用初始化清单和直接在构造函数内初始化成员变量的区别
- C/C++ 通过初始化列表和构造函数内赋值初始化成员变量的区别
- C/C++ 通过初始化列表和构造函数内赋值初始化成员变量的区别
- c++中构造函数初始化的方法以及主要区别
- C/C++ 通过初始化列表和构造函数内赋值初始化成员变量的区别
- C/C++ 通过初始化列表和构造函数内赋值初始化成员变量的区别
- C/C++ 通过初始化列表和构造函数内赋值初始化成员变量的区别
- C++中构造函数初始化列表与构造函数体的区别
- 构造函数初始化列表和构造函数的区别
- C++ 初始化列表与构造函数的区别