如何创建一个可以在流中下一个对象中调用特定函数的操纵器?
假设我有一个如下的类:
class A
{
...private members
public:
void write_text(std::ostream& os); //writes members as text
void write_binary(std::ostream& os); //writes objects as binary
};
如何创建一个类似于 text
和 binary
的操纵器,具体取决于我可以调用适当的函数 write_text()
或 write_binary()
写入文件流,如下所示:
std::ofstream file1("textfile.txt");
std::ofstream file2("binfile.bin");
A obj; // assume obj has data members set
file1<<text<<obj; // here obj.write_text() should be invoked
file2<<binary<<obj; // here obj.write_binary() should be invoked
我是否需要在流中存储诸如状态或变量之类的内容,如下所示 示例能够执行此操作或者是否存在更简单的方法?
Suppose I have a class as follows:
class A
{
...private members
public:
void write_text(std::ostream& os); //writes members as text
void write_binary(std::ostream& os); //writes objects as binary
};
How do I create a manipulator that like text
and binary
depending on which I can call appropriate function write_text()
or write_binary()
to write to filestream like so:
std::ofstream file1("textfile.txt");
std::ofstream file2("binfile.bin");
A obj; // assume obj has data members set
file1<<text<<obj; // here obj.write_text() should be invoked
file2<<binary<<obj; // here obj.write_binary() should be invoked
Do I need to store something like a state or a variable in the stream like in this example to be able to do this or is there a simpler way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
标准用法操纵输入&amp;的主要方式有两种主要方法。输出操作。
1。在流状态中存储值
您可以使用 <代码> std :: ios_base :: Xalloc() 。
这为您提供了
long
和void*
在每个流中的值,您可以使用iword()
/pword()
。这与标准IO操纵器等标准的机制相同/code> ,
std :: boolalpha
使用。请注意,如果您更改流状态,它将一直保持这种状态,直到您再次更改它,例如:
您可以为您的
a
类实现它:运营商&lt;&lt; << /code> for
a
检查当前存储在流中的格式类型(文本为0,二进制1),并调用相应的方法text> text
&amp;二进制
是将流状态应用于流时更改流状态的IO操纵器。示例用法:
godbolt示例示例
2。包装器功能函数
,您还会在标准中遇到另一种类型的IO手工库是更改单个元素的输入 /输出的包装器。
其中的示例将为
std ::引用
,std :: get_money
,std :: put_money
等...这些功能仅更改单个操作的格式,与上述方法更改所有以下输入 /输出操作的格式相比。
示例:
您可以为您的
a
类实现它:binary_impl
),该对象实现了不同的ocerator&lt;&lt; 用于
a
对象。示例用法:
godbolt示例示例
上面列出的方法只是标准库本身使用(因此可能可能是标准库本身最公认的)。
当然,您还可以为其创建自己的自定义方法,例如,使用返回将以特定方式序列化对象的对象的成员方法:
<
a href =“ https://godbolt.org/z/skxwcexan” rel =“ nofollow noreferrer”> godbolt示例
There are two primary ways the standard uses to manipulate input & output operations.
1. Storing values in the stream state
You can store formatting state within streams by using
std::ios_base::xalloc()
.This gives you a
long
andvoid*
value in each stream that you can access withiword()
/pword()
.This is the same mechanism that standard io manipulators like
std::hex
,std::boolalpha
use.Note that if you change the stream state it'll stay that way until you change it again, e.g.:
You could e.g. implement it like this for your
A
class:operator<<
forA
checks which format type is currently stored in the stream (0 for text, 1 for binary) and calls the corresponding methodtext
&binary
are the io manipulators that change the stream state when applied to a stream.Example Usage:
godbolt example
2. Wrapper function
Another kind of io manipulators you'll also encounter in the standard library are wrappers that change the input / output of a single element.
Examples of this would be
std::quoted
,std::get_money
,std::put_money
, etc...Those functions only change the format for a single operation, in contrast to the above method that changes the format of all following input / output operations.
Example:
You could e.g. implement it like this for your
A
class:binary_impl
) that implements a differentoperator<<
forA
objects.Example Usage:
godbolt example
The methods listed above are only the ones the standard library itself uses (and therefore probably the most recognized ones).
You can of course also create your own custom method for it, e.g. by using member methods that return objects that will serialize the object in a specific way:
Usage:
godbolt example