在 c++ 中创建函数输出到指定源

发布于 2024-12-06 18:51:48 字数 422 浏览 0 评论 0原文

我想要一个函数,将某些信息输出到输入到该函数的特定指定源。在代码中,我的意思是:

function output( source ) {

source << "hello" << endl;

}

其中 source 可以是 ofstreamcout。这样我就可以像这样调用这个函数:

output(cout)ofstream otp ("hello");输出(otp)

我的问题是,如何描述source 以使这项工作正常进行?可以公平地假设 source 始终是 std 类的成员,

谢谢!

I want to have a function that outputs certain pieces of information to a specific designated source that is inputted to the function. In code, what I mean is:

function output( source ) {

source << "hello" << endl;

}

where source can be a ofstream or cout. So that I can call this function like so:

output(cout) or ofstream otp ("hello"); output(otp)

My question is, how do I characterize source to make this work? It's fair to assume that source will always be a member of the std class

Thanks!

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

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

发布评论

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

评论(5

江心雾 2024-12-13 18:51:49
void output(std::ostream &source) {
    source << "hello" << std::endl;
}

甚至:

template <T>
void output(T &source) {
    source << "hello" << std::endl;
}
void output(std::ostream &source) {
    source << "hello" << std::endl;
}

or even:

template <T>
void output(T &source) {
    source << "hello" << std::endl;
}
给妤﹃绝世温柔 2024-12-13 18:51:49

将你的函数写为:

std::ostream& output(std::ostream& source )
{
  return source << "hello" << endl;
}

然后你可以使用它:

output(cout);

//and 
ofstream otp ("hello"); 
output(otp);

//and
output(output(cout));
output(output(output(cout)));
output(output(output(output(cout))));

//and even this:
output(output(output(output(cout)))) << "weird syntax" << "yes it is" ;

顺便说一句,如果 output 函数有很多行,那么你可以将它写为:

std::ostream& output(std::ostream& source )
{
  source << "hello" << endl;
  source << "world" << endl;
  //....
  return source;
}

重点是它应该返回 source< /代码>。在早期版本中,该函数返回source

Write your function as:

std::ostream& output(std::ostream& source )
{
  return source << "hello" << endl;
}

Then you can use it as:

output(cout);

//and 
ofstream otp ("hello"); 
output(otp);

//and
output(output(cout));
output(output(output(cout)));
output(output(output(output(cout))));

//and even this:
output(output(output(output(cout)))) << "weird syntax" << "yes it is" ;

By the way, if the output function has many lines, then you can write it as:

std::ostream& output(std::ostream& source )
{
  source << "hello" << endl;
  source << "world" << endl;
  //....
  return source;
}

The point is that it should return source. In the earlier version, the function returns source.

她如夕阳 2024-12-13 18:51:49

您应该传递 std::ostream& 作为参数

You should pass an std::ostream& as argument

或十年 2024-12-13 18:51:49
function output( source ) {
  source << "hello" << endl;
}

如果这是一个成员函数,其目的是转储有关它所属类的对象的数据,请考虑将其重命名为operator<<。因此,不要

class Room {
  ...
  // usage myRoom.output(otp)
  void output(std::ostream& stream) {
    stream << "[" << m_name << ", " << m_age << "]";
  }
};

尝试这样做:

class Room {
  ...
  // usage opt << myRoom << "\n"
  friend std::ostream& operator<<(std::ostream& stream, const Room& room) {
    return stream << "[" << room.m_name << ", " << room.m_age << "]";
  }
};

这样,您可以使用更自然的语法显示类的状态:

std::cout << "My Room: " << myRoom << "\n";

而不是笨重的语法

std::cout << "My Room: ";
myRoom.output(std::cout);
std::cout << "\n";
function output( source ) {
  source << "hello" << endl;
}

If this is a member function, the point of which is to dump data about objects of the class of which it is a member, consider renaming it to operator<<. So, instead of

class Room {
  ...
  // usage myRoom.output(otp)
  void output(std::ostream& stream) {
    stream << "[" << m_name << ", " << m_age << "]";
  }
};

rather, try this:

class Room {
  ...
  // usage opt << myRoom << "\n"
  friend std::ostream& operator<<(std::ostream& stream, const Room& room) {
    return stream << "[" << room.m_name << ", " << room.m_age << "]";
  }
};

That way, you can display the state of your class using a more natural syntax:

std::cout << "My Room: " << myRoom << "\n";

instead of the klunky

std::cout << "My Room: ";
myRoom.output(std::cout);
std::cout << "\n";
Hello爱情风 2024-12-13 18:51:49

恕我直言,重定向输出应该在用户级别完成。像这样编写你的 C++:

cout << "hello" << endl;

当执行应用程序时,用户可以将输出重定向到他想要的任何内容,例如文件:

myapp > myfile

IMHO, redirecting output should be done at the user level. Write your C++ like this:

cout << "hello" << endl;

And when executing the application, user can redirect the output to whatever he wants, say a file:

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