读/写函数定义
您将如何编写这些函数:
bool writesomething(const Something& something);
//or
bool writesomething(int somethingid, const Something& something);
bool readsomething(const Something& something);
//or
bool readsomething(int somethingid, Something& something);
请注意 Something 有一个名为 id 的公共字段
how would you write these functions:
bool writesomething(const Something& something);
//or
bool writesomething(int somethingid, const Something& something);
bool readsomething(const Something& something);
//or
bool readsomething(int somethingid, Something& something);
notice that Something has a public field named id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于 Something 的结构。但作为一般设计原则,您的类应该尽可能保持无知。
话虽这么说,如果您希望能够对对象进行(反)序列化,我会在内置 I/O 流库接口上对您的 I/O 方法进行建模。然后,您的对象将支持与控制台、文件以及从
istream
或ostream
继承的任何其他内容之间的 I/O。除了与标准库(以及大量第三方代码)保持一致的接口之外,调整 I/O 流接口还可以让您更轻松地编写方法。如果 Something 有一个 int 'id' 和一个字符串 'name' 成员:
如果 Something 的成员更复杂,则可以实现运算符 <<和>>对于这些类型,依此类推。
That depends on the structure of Something. But as a general design principle, your classes should be persistence ignorant when possible.
That being said, if you want to be able to (de)serialize your objects, I would model your I/O methods on the built-in I/O streams library interfaces. Then your objects will support I/O to/from the console, files, anything else that inherits from
istream
orostream
.In addition to maintaining a consistent interface with the standard library (and tons of third-party-code), adapting the I/O streams interface also makes it easier to write your method. If Something has an int 'id' and a string 'name' member:
If the members of Something are more complex you implement operator << and >> for those types, and so on down the chain.