指向 COM 对象的成员变量指针
在 C++ 中保留对 COM 对象的成员变量指针引用并在整个类中重用该引用是否存在任何问题?
有人知道为什么每次想要使用 COM 对象时都想要调用 .CreateInstance 吗,即每次都得到一个新的实例。
我看不出您有任何理由想要这样做,
谢谢,
(“不”是一个可以接受的答案!!!)
Is there any problem with keeping member variable pointer refernces to COM objects and reussing the reference through out the class in C++.
Is anybody aware of a reason why you would want to call .CreateInstance every time you wanted a to use the COM object i.e. you were getting a fresh instance each time.
I cannot see any reason who you would want to do this,
Thanks,
(No is an acceptable answer!!!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于你真正想要什么。
如果每次都需要相同的对象,则必须保留指向它的指针。如果您每次都需要一个新对象(无论出于何种原因),您每次都必须创建一个新实例。如果您不介意保留该对象,则更好,因为调用
CoCreateInstance()
的成本相对较高。It depends on what you really want.
If you need the same object every time you have to keep a pointer to it. If you need a new object every time (for whatever reason) you have to create a new instance each time. If you don't care keeping the object is preferable because calls to
CoCreateInstance()
are relatively expensive.在这种情况下没有通用规则,因为有许多变量决定它是否是一个好主意。
第一:如果您拥有相关的 COM 对象,即拥有源代码并控制它们的使用方式,那么是的,它是完全安全的。
如果 COM 对象是第 3 方 COM 对象,有时其中的蹩脚代码可能会迫使您在每次使用它们时“createInstance”——出于必要(和自我保存)。
如果 COM 对象充当代理对象,则您可能需要在每次使用它们时创建它们,因为幕后的东西(即其他客户端使用同一对象)。
还有更多情况,但总结一下:这取决于......
There is no general rule in this case because there are a number of variables that decide whether it is a good idea or not.
First: If you own the COM objects in question i.e. have source code and control over how they are used, then yes its perfectly safe.
If COM objects are 3rd party COM objects sometimes crappy code in them may force you to "createInstance" on them every time you use them - out of necessity (and self preservation).
If the COM object is acting as a proxy object you may need to create them every time you use them because of stuff behind the scene i.e. other clients using the same object.
there are more situations, but to summarize: it depends...
我想说这取决于 COM 对象是什么以及您如何使用它。重用 ADO 连接通常没问题,但如果将其置于脏状态,那么在重用它时可能会遇到奇怪的行为。某些 COM 对象可能具有重新初始化或清除方法,您可以调用将它们重置回干净状态。
I would say it depends on what the COM object is and how you use it. It is generally fine to reuse an ADO connection, but if you leave it in a dirty state then you may encounter odd behavior when you reuse it. Some COM object may have a re-initialize or clear method you can call to reset them back to a clean state.