C++ 输出流库 - 哨兵



描述

它用于准备输出流。所有执行输出操作的成员函数都会自动构造此类的一个对象,然后对其进行评估(如果未设置任何状态标志,则返回 true)。只有当此对象评估为 true 时,函数才会尝试执行输出操作(否则,它会在不执行操作的情况下返回)。在返回之前,函数会销毁哨兵对象。

声明

以下是 std::basic_ostream::sentry 的声明。

C++98

class sentry {
   public:
      explicit sentry (basic_ostream& os);
      ~sentry();
      operator bool() const;
   private:
      sentry (const sentry&);
      sentry& operator= (const sentry&);
};

C++11

class sentry {
   public:
      explicit sentry (basic_ostream& os);
      ~sentry();
      explicit operator bool() const;
      sentry (const sentry&) = delete;
      sentry& operator= (const sentry&) = delete;
};

成员

  • explicit sentry (basic_istream& is, bool noskipws = false); − 准备输出流以进行输出操作,执行上面描述的操作。

  • ~sentry(); − 不执行任何操作(实现定义)。

  • explicit operator bool() const; − 当评估对象时,它返回一个 bool 值,指示哨兵构造函数是否成功执行了所有任务:如果在构造过程的某个点设置了内部错误标志,则此函数始终对该对象返回 false。

ostream.htm
广告

© . All rights reserved.