c++ - C++ 中的 "sentry object"是什么?

标签 c++ idioms

answered这个question , 和 Potatoswatter answered也一样

The modern C++ equivalent would be a sentry object: construct it at the beginning of a function, with its constructor implementing call(), and upon return (or abnormal exit), its destructor implements

我不熟悉在 C++ 中使用哨兵对象。 我认为它们仅限于输入和输出流。

有人可以向我解释一下 C++ 哨兵对象以及如何将它们用作类中一个或多个方法的环绕拦截器吗?

即如何做到这一点?

Sentry objects are very similar indeed. On the one hand they require explicit instantiation (and being passed this) but on the other hand you can add to them so that they check not only the invariants of the class but some pre/post conditions for the function at hand.

最佳答案

Sentry 对象是一种模式,但我不确定以下哪个(可能全部)。

C++ 程序通常严重依赖于对象(可能是用户定义的类)何时被销毁,即当其析构函数被调用时。对于具有垃圾收集功能的语言,情况并非如此。

此技术用于,例如,包含“资源获取即初始化”范式:在调用对象构造函数时获取资源,编译器会自动调用其析构函数以在正常和异常(异常)情况下释放资源(查看 this question)。

您可以利用 build /销毁时间知识的常见地方是

  • block :在 block 的末尾调用“堆栈分配”对象的析构函数

    void function()
    {  Class foo = Object(resource);
       other_operations();
    }  // destructor for foo is called here
    
  • 函数调用:调用函数时也会发生“堆栈分配”

    void function()
    {  another_function ( Class(resource)  );
       // destructor for the unnamed object is called
       // after another_function() returns (or throws)
       other_operations();
    }
    
  • 包含对象的构造/销毁:

    class Foo
    {  Class sentry;
       public: Foo()
       { // Constructor for sentry is called here
          something();
       }        
       public: ~Foo()
       {
          something();
       }  // destructor for sentry is called here
    };
    

在 STL 中有一个名为 sentry 的类(更准确地说是 istream::sentry),它实现了上述模式的第三种模式。所以我认为这就是一些程序员所说的“哨兵对象”。

但实际上上述Class类的任何对象都可以称为“哨兵对象”。它们是“哨兵”,因为它们确保即使某些东西抛出异常也不会错过这些难以捉摸的对象析构函数(因此它们就像 block /类的守护者一样)。

更多哨兵对象示例在RAII question .


您可以看到与面向方面编程的关系;这些对象类似于“方面”,具有“在封闭 block 的开始/结束处”、“在包含对象的构造/销毁处”等的切点。但是这些“方面”必须出现在他们方面的代码。因此,与原始 call/return 功能相比,它们的“外观”更少;相反,应该在类的每个函数中插入一个哨兵对象:

class X{
  struct Sentry {
     Sentry() { /* call() */}
    ~Sentry() { /* return() */};
  };

  void member_function()
  { Sentry();
    /* operations */
  }

  void another_member_function()
  { Sentry();
    /* operations */
  }
};

关于c++ - C++ 中的 "sentry object"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2690851/

相关文章:

python - 两个列表值的组合

c++ - 是否有与 BUCK 的 header_namespace 等效的 cmake?

c++ vectors - 使用 find(begin, end, term)

Python 等价于 Perl 的习语 do this or that,通常称为 "or die"?

file-io - 用 Common Lisp 就地替换文件中的正则表达式

python - 如果 a.) 键不存在或 b.) 键未验证,则以最惯用的方式安全地从 dict() 中弹出一个键

function - 在 Clojure 中传递方法名称以进行评估的惯用方法?

c++ - 为什么在复制对象时忽略顶级常量?

c++ - 从 QMenu 中删除图标空间

c++ - 使用元编程选择成员变量