指针指向指针的问题

发布于 2024-07-15 07:56:16 字数 423 浏览 9 评论 0原文

我有一个带有指向接口对象(让我们称之为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故事未完 2024-07-22 07:56:16

使用指向指针的指针是指任一类都可以更改指针的值 - 例如,通过删除现有对象并用新对象替换它。 这允许两个类通过取消引用指针到指针来仍然使用同一对象。

如果不是,您关心的是确保该对象在两个类的整个生命周期中保持有效。

  • 如果嵌套类的寿命较短,您不必担心。
  • 如果是相同的,只要您按照正确的顺序进行清理(例如首先嵌套类,然后对象),那么您不必担心
  • 如果嵌套类在所有者被销毁后可以持续存在,那么您必须实现一种方法以确保该对象也持续存在。

如果您需要确保对象的生命周期,可以通过手动或通过智能指针接口通过引用计数语义来完成。

对于智能指针来说 boost::shared_ptr 将是一个不错的选择。 shared_ptr 允许一个对象的所有权被多个指针共享。 当最后一个shared_ptr超出范围时,该对象将被删除。

(请注意,auto_ptr 的情况并非如此,其中对象是独占拥有的)。

需要注意的事项;

  1. 当使用 boost::shared_ptr 时,请确保嵌套类具有 shared_ptr 的副本,而不是引用/指针。
  2. std::auto_ptr 的行为完全不同,对象是独占的,而不是共享的
  3. boost::shared_ptr 只能与堆对象一起使用,例如从调用“new”返回的指针示例

typedef boost::shared_ptr<Interface> shared_interface;

class NestedClass
{
  shared_interface mInterface; // empty pointer
}

void NestedClass::setInterface(shared_interface& foo)
{
  mInterface= foo; // take a copy of foo.
}

void ParentClass::init( void )
{
  // mInterface is also declared as shared_interface
  mInterface = new Interface();
  mNestedClass->setInterface(mInterface);
}

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 the nested class lives shorter you don't really have to worry.
  • If it's the same, provided you clean-up in the correct order (e.g. nested class first, object later) then again, you don't have to worry
  • If the nested class could persist after the owner is destroyed then you must implement a way to ensure the object also persists.

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;

  1. When using boost::shared_ptr be sure the nested class has a copy of the shared_ptr and not a reference/pointer.
  2. std::auto_ptr behaves quite differently, objects are exclusively owned and not shared
  3. boost::shared_ptr can only work with heap objects, e.g pointers returned from a call to 'new'

Example:

typedef boost::shared_ptr<Interface> shared_interface;

class NestedClass
{
  shared_interface mInterface; // empty pointer
}

void NestedClass::setInterface(shared_interface& foo)
{
  mInterface= foo; // take a copy of foo.
}

void ParentClass::init( void )
{
  // mInterface is also declared as shared_interface
  mInterface = new Interface();
  mNestedClass->setInterface(mInterface);
}
陌伤ぢ 2024-07-22 07:56:16

您可能想要使用指向指针的指针的另一个原因是,如果外部代码可能会更改原始指针值(例如,使其指向一个新对象,或者在释放该对象后将其设置为 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).

坚持沉默 2024-07-22 07:56:16

传入指向您的接口(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.

寒江雪… 2024-07-22 07:56:16

基本上,您在两个不同的对象之间共享指向同一对象的指针。 如果您没有使用任何智能指针,只需将指针存储到共享对象即可。 您必须小心共享对象的所有权,即哪个对象负责释放共享对象并通知其他对象它已经消失。

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.

抚你发端 2024-07-22 07:56:16
class Outer
{
    class Inner
    {
    };
};

由于 Outer 对象仅保存指向 pInterface 对象的 RAW 指针,这意味着 Outer 对象不拥有或无法控制 pInterface 对象的生命周期。 因此,我们希望能够保证 pInterface 对象的生存时间与外部对象一样长; 在这种情况下,甚至没有理由使用指针,您可以只使用引用(假设不存在 pInterface 为 NULL 的情况)。

Inner 如何保存其“引用”(不是 C++ 引用)取决于我们确实需要有关所涉及对象之间关系的更多信息!

  • 内部对象和外部对象之间的关系是什么?
  • 内部对象相对于它继承 pInterface 指针的外部对象的生命周期是多少?
  • 如何保证 Outer 对象的生命周期比 pInterface 对象短。

ETC。

class Outer
{
    class Inner
    {
    };
};

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!

  • What is the realationship between Inner and Outer objects.
  • What is the lifespan of an Inner object in relation to the Outer object that it inherited the pInterface pointer from?
  • What is the guarantee that an Outer object has a lifespan shorter than a pInterface object.

etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文