指针指向指针的问题
我有一个带有指向接口对象(让我们称之为 pInterface)的(非智能)指针的类,并且我正在构建一个也需要访问该接口的嵌套类。 我将通过将指向接口的指针传递到嵌套类的构造函数中来解决这个问题,如下所示:
CNestedClass someClass( pInterface, ... );
但是我不确定将此指针存储在嵌套类中的最佳方法。 我可以使用:
1) A scoped (or other smart) pointer (to the original object)
2) A pointer to a pointer
你们会建议什么,为什么?
编辑:我应该澄清 - 嵌套类需要调用接口对象上的方法,但它不会创建它(或修改“指向”的对象),父类负责这一点。
I have a class with a (non smart) pointer to an interface object (lets call it pInterface) and I am building a nested class which also needs access to that interface. I am going to get around this by passing the pointer to the interface into the constructor of the nested class like so:
CNestedClass someClass( pInterface, ... );
However I am unsure of the best way of storing this pointer in the nested class. I could use:
1) A scoped (or other smart) pointer (to the original object)
2) A pointer to a pointer
What would you guys suggest and why?
EDIT: I should clarify - the nested class will need to call methods on the interface object, however it does not create it (or modify the object 'pointed' to ), the parent class is responsible for that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用指向指针的指针是指任一类都可以更改指针的值 - 例如,通过删除现有对象并用新对象替换它。 这允许两个类通过取消引用指针到指针来仍然使用同一对象。
如果不是,您关心的是确保该对象在两个类的整个生命周期中保持有效。
如果您需要确保对象的生命周期,可以通过手动或通过智能指针接口通过引用计数语义来完成。
对于智能指针来说 boost::shared_ptr 将是一个不错的选择。 shared_ptr 允许一个对象的所有权被多个指针共享。 当最后一个shared_ptr超出范围时,该对象将被删除。
(请注意,auto_ptr 的情况并非如此,其中对象是独占拥有的)。
需要注意的事项;
:
The use a pointer to a pointer is if either class may alter the value of the pointer - e.g. by deleting the existing object and replacing it with a new one. This allows both classes to still use the same object by dereferencing the pointer-to-pointer.
If not your concern is ensuring the object remains valid throughout the lifetime of both classes.
If you need to ensure the lifetime of the object it could be done via reference counting-semantics, either manually or through a smart-pointer interface.
For a smart pointer then boost::shared_ptr would be a good choice. shared_ptr allows the ownership of an object to be shared amount multiple pointers. When the last shared_ptr goes out of scope, the object is deleted.
(note this is not the case with auto_ptr, where an object are exclusively owned).
Things to be aware of;
Example:
您可能想要使用指向指针的指针的另一个原因是,如果外部代码可能会更改原始指针值(例如,使其指向一个新对象,或者在释放该对象后将其设置为 NULL)。它指向的对象)。 然而,在我看来,将指针交给其他人后更改它是非常糟糕的做法。
因此,如果外部代码和嵌套类都没有更改指针,只需将其作为原始指针的副本作为成员变量(字段)存储在嵌套类中即可。
The other reason you may want to use a pointer to a pointer would be if the outer code might change the original pointer value (e.g. make it point to a new object, or set it to NULL after releasing the object it points to). However, IMO it is quite bad practice to change a pointer after giving it to someone else.
So, if neither the outer code nor the nested class changes the pointer, just store it in the nested class as a copy of the original pointer as a member variable (field).
传入指向您的接口(IMyInterface**ppInterface)的指针的地址,如果该指针是由类实现的,则填充该指针。
该类可以将其 this 指针转换为该接口并填充指针 *ppInterface。
如果该类未实现此接口,则可以将 *ppInterface 设置为 NULL。
Pass in the address of a pointer to your interface (IMyInterface**ppInterface), and fill the pointer if it is implemented by the class.
The class can cast its this pointer to that interface and fill in the pointer *ppInterface.
If the class does not implement this interface it can set *ppInterface to NULL.
基本上,您在两个不同的对象之间共享指向同一对象的指针。 如果您没有使用任何智能指针,只需将指针存储到共享对象即可。 您必须小心共享对象的所有权,即哪个对象负责释放共享对象并通知其他对象它已经消失。
Basically, you are sharing a pointer to the same object between two different objects. If you are not using any smart pointers, simply store the pointer to the shared object. You must be careful about ownership of the shared object, i.e. which object is responsible to deallocate the shared object and notify the others that it's gone.
由于 Outer 对象仅保存指向 pInterface 对象的 RAW 指针,这意味着 Outer 对象不拥有或无法控制 pInterface 对象的生命周期。 因此,我们希望能够保证 pInterface 对象的生存时间与外部对象一样长; 在这种情况下,甚至没有理由使用指针,您可以只使用引用(假设不存在 pInterface 为 NULL 的情况)。
Inner 如何保存其“引用”(不是 C++ 引用)取决于我们确实需要有关所涉及对象之间关系的更多信息!
ETC。
As an object of Outer only holds a RAW pointer to a pInterface object this implies that the Outer object does not own or have any control over the lifespan of the pInterface object. We therefore hope there is some guarantee that the pInterface object will live as long as the outer object; In this case there is no reason to even use a pointer you could just use a reference (assuming there is no situation that pInterface would be NULL).
How Inner holds its 'reference' (not C++ reference) depends and really we need more information about the relationship between the objects involved!
etc.