如何解决模板问题以将不同的数据类型保存到文件?
我在使用模板时遇到了一个新问题。这是我最近读的一本书中的一点创意,扩展了他的想法,它有这个代码示例。
假设您有一个模板化的 Array2D 类。你有这个方法(Array2D::WriteFile):
bool WriteFile( const char* p_filename )
{
FILE* outfile = 0;
int written = 0;
// open the file
outfile = fopen( p_filename, "wb" );
// return if it couldn't be opened
if( outfile == 0 )
return false;
// write the array and close thef ile
written = fwrite( m_array, sizeof( Datatype ), m_size, outfile );
fclose( outfile );
// if we didn't write the number of items we expected,
// return failure
if( written != m_size )
return false;
// return success.
return true;
}
这对于基本数据类型(如 int、char、float 等)效果很好。但是,如果你有一点创意,使数据类型成为复合变量,如 SLinkedList(单链接),该怎么办?列表)。它将是Array2D。然后调用旧的 WriteFile() 函数。它会保存它,但你只保存了一个节点。其余节点被遗忘,再也没有回来。
我的脑海中浮现出一些想法: 1. 转动你的拇指,看着时间快要到最后期限了。 2. 将 Array2D::WriteFile() 设为纯虚函数,使更具体的类以应有的方式保存它。但我无法在其他独立情况下单独使用 Array2D 。 3.编写一个函数指针来保存,但我认为它可能会变得混乱,因为直到时间到来你都不知道你传递了什么数据。它可能因数据类型而异。 4. 意识到模板也许不是解决方案。
模板类并不能根据数据类型为我解决每种情况。那么您认为什么是好的解决方案呢?谢谢!
I came a across a new problem when using templates. This is me being a little creative from a book I recently read, expanding his ideas, and it has this code example.
Say you have a templated Array2D class. And you have this method (Array2D::WriteFile):
bool WriteFile( const char* p_filename )
{
FILE* outfile = 0;
int written = 0;
// open the file
outfile = fopen( p_filename, "wb" );
// return if it couldn't be opened
if( outfile == 0 )
return false;
// write the array and close thef ile
written = fwrite( m_array, sizeof( Datatype ), m_size, outfile );
fclose( outfile );
// if we didn't write the number of items we expected,
// return failure
if( written != m_size )
return false;
// return success.
return true;
}
This works fine for basic data types such as int, char, float, etc. But what if you get a little creative making the data type a compound variable such as SLinkedList (singly linked list). It would be Array2D. Then you call your old WriteFile() function. It'll save it, but you just only saved one node. The rest of the nodes went into oblivion never to return.
A few ideas came in my head:
1. Twiddle your thumbs watching the clock go by towards your deadline.
2. Make Array2D::WriteFile() a pure virtual function causing a more specific class to save it the way it should. But then I can't use Array2D by itself in other stand-alone situations.
3. Write a function pointer to save, but I think it could get messy because you don't know what data your passing into til the time comes. It could vary based on the data type.
4. Realize templates aren't the solution, perhaps.
The template class isn't solving every case scenario for me based on the data type. So what would be a good solution in your opinion? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的对象需要可序列化。想想当您尝试以这种方式写出链表的头部时,实际上会发生什么 - 当然,它只写了第一个节点; write 函数不知道它在写什么,因此它不知道如何跟随指向下一个节点的指针并写入该节点。它只是看到位并将它们写出,直到当前对象的末尾,在这种情况下,这只是第一个节点。
序列化基本上意味着,对于每种重要类型,编写一种方法,将该类型打包成适合写出的扁平字节流(理想情况下,还有第二种方法可以读回该格式)。
现在,您的问题被标记为 C++,但您编写的代码非常类似于 C。在 C++ 中实现可序列化对象的正常方法是使用
std::ostream&
参数和返回值覆盖<<
运算符,例如对称地
现在 ,你可以这样做:
请注意,这种覆盖流运算符的方法意味着,如果
MyType::field2
碰巧本身是一个复杂的对象,它仍然会被正确地序列化和反序列化在上面的代码中,只要其流运算符被覆盖。但如果您想像原来的帖子一样继续使用 C 风格的文件 I/O,事情就不会那么干净了。如果您的代码确实应该是 C++,那么您应该使用 iostream 库来执行文件 I/O。
Your objects need to be serializable. Think about what actually happens under the hood when you try to write out the head of a linked list this way - of course it just writes the first node; the
write
function has no idea what it's writing, so it has no idea how to follow the pointers to the next node and write that too. It just sees bits and writes them out until the end of the current object, which is in that case just the first node.Serialization basically means, for each non-trivial type, writing a method that packages up that type into a flat stream of bytes suitable for writing out (and ideally also a second method that reads that format back in).
Now, your question is tagged C++, but the code you've written is very C-like. The normal way to implement a serializable object in C++ is to override the
<<
operator with astd::ostream&
argument and return value, e.g.And symmetrically
Now, you can just do something like this:
Note that this approach of overriding the stream operators means that if, for example
MyType::field2
happens to itself be a complex object, it will still be serialized and deserialized properly in the above code as long as its stream operators are overridden.But if you want to continue using C-style file I/O as in your original post, things aren't going to be quite so clean. If your code is really supposed to be C++ though, you should be using the iostream libraries to do your file I/O.
您可以简单地使用 iostreams 等效项。不要使用
fwrite
(它是一个 C 函数并且仅适用于 C 数据类型),而是使用可以重载并专门用于自定义类型的 C++ 等效函数。运算符<<
适用于所有内置类型,对于自定义类型,您可以自己定义。You could simply use the
iostreams
equivalent. Instead of usingfwrite
which is a C function and only works with C datatypes, use the C++ equivalent which can be overloaded and specialized for custom types.operator <<
works for all built-in types, and for custom types, you can define it yourself.我的第一反应是,你正在研究一个以前已经研究过(并且至少在某种程度上已经解决)的问题。是的,它仍然可以大量使用模板。我会看看
std::copy
和std::ostream_iterator
以及一些序列化库,例如 Boost 序列化。My immediate reaction is that you're looking at a problem that's been looked at (and solved, to at least some degree) before. Yes, it can still use templates pretty heavily. I'd take a look at
std::copy
along withstd::ostream_iterator
, as well as some serialization libraries, such as Boost serialization.