为特定类创建 ostream 操纵器

发布于 2024-08-29 10:56:28 字数 893 浏览 7 评论 0原文

我有一个从 ostream 派生的类:

class my_ostream: public std::ostream
{
    // ...
}

我想制作一个专门针对此类的操纵器(例如 do_something),如下所示:

my_ostream s;
s << "some text" << do_something << "some more text";

我做了以下操作:

std::ostream &do_something(std::ostream &os)
{
    my_ostream *s = dynamic_cast<my_ostream*>(&os);
    if (s != NULL)
    {
        // do something
    }
    return os;
}

这有效,但更确切地说丑陋的。我尝试了以下方法:

my_ostream &do_something(my_ostream &s)
{
    // do something
    return s;
}

这不起作用。我还尝试了另一种方法:

class my_ostream: public std::ostream
{
   // ...
   my_ostream &operator<<(const do_something & x)
   {
       // do something
       return *this;
   }
}

这仍然不起作用。

I have a class that is derived from ostream:

class my_ostream: public std::ostream
{
    // ...
}

I want to make a manipulator (for example do_something), that works specifically to this class, like this:

my_ostream s;
s << "some text" << do_something << "some more text";

I did the following:

std::ostream &do_something(std::ostream &os)
{
    my_ostream *s = dynamic_cast<my_ostream*>(&os);
    if (s != NULL)
    {
        // do something
    }
    return os;
}

This works, but is rather ugly. I tried the following:

my_ostream &do_something(my_ostream &s)
{
    // do something
    return s;
}

This doesn't work. I also tried another approach:

class my_ostream: public std::ostream
{
   // ...
   my_ostream &operator<<(const do_something & x)
   {
       // do something
       return *this;
   }
}

This still doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

哭了丶谁疼 2024-09-05 10:56:28

您需要在类中添加对操纵器的支持:

#include<iostream>
class my_ostream : public std::ostream
{
    public:
    std::string prefix;

    my_ostream():prefix("*"){}

    // manipulator support here:
    my_ostream& operator<<( my_ostream&(*f)(my_ostream&)){
        f(*this);
        return *this;
    }
};

my_ostream& operator<<(my_ostream &st, const std::string &s){
      std::cout << st.prefix << s;
      return st;
}

// manipulator: clear prefix
my_ostream& noprefix(my_ostream &st){
   st.prefix="";
}

int main(){
  my_ostream s;
  std::string str1("text");
  std::string str2("text");
  s << str1 << noprefix << str2;
}

You need to add support for manipulators in your class:

#include<iostream>
class my_ostream : public std::ostream
{
    public:
    std::string prefix;

    my_ostream():prefix("*"){}

    // manipulator support here:
    my_ostream& operator<<( my_ostream&(*f)(my_ostream&)){
        f(*this);
        return *this;
    }
};

my_ostream& operator<<(my_ostream &st, const std::string &s){
      std::cout << st.prefix << s;
      return st;
}

// manipulator: clear prefix
my_ostream& noprefix(my_ostream &st){
   st.prefix="";
}

int main(){
  my_ostream s;
  std::string str1("text");
  std::string str2("text");
  s << str1 << noprefix << str2;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文