c++将未知类型传递给函数和任何类类型定义
我正在尝试创建一个通用类来向文件写入和读取对象。 称之为 ActiveRecord 类
只有一个方法,该方法保存类本身:
void ActiveRecord::saveRecord(){
string fileName = "data.dat";
ofstream stream(fileName.c_str(), ios::out);
if (!stream) {
cerr << "Error opening file: " << fileName << endl;
exit(1);
}
stream.write(reinterpret_cast<const char *> (this), sizeof(ActiveRecord));
stream.close();
}
现在我用 User 类扩展此类:
class User : public ActiveRecord
{
public:
User(void);
~User(void);
string name;
string lastName;
};
创建并保存用户我想做一些类似的事情:
User user = User();
user.name = "John";
user.lastName = "Smith"
user.save();
如何获取此 ActiveRecord::saveRecord( ) 方法接受任何对象和类定义,因此它会写入我发送给它的任何内容:
看起来像:
void ActiveRecord::saveRecord(foo_instance, FooClass){
string fileName = "data.dat";
ofstream stream(fileName.c_str(), ios::out);
if (!stream) {
cerr << "Error opening file: " << fileName << endl;
exit(1);
}
stream.write(reinterpret_cast<const char *> (foo_instance), sizeof(FooClass));
stream.close();
}
当我们这样做时,c++ 中的默认对象类型是什么。 例如。 在 Objective-C 中它是 id 在java中它是对象 在AS3中它是对象 C++ 里的是什么??
I am trying to create a generic class to write and read Objects to/from file.
Called it ActiveRecord class
only has one method, which saves the class itself:
void ActiveRecord::saveRecord(){
string fileName = "data.dat";
ofstream stream(fileName.c_str(), ios::out);
if (!stream) {
cerr << "Error opening file: " << fileName << endl;
exit(1);
}
stream.write(reinterpret_cast<const char *> (this), sizeof(ActiveRecord));
stream.close();
}
now I'm extending this class with User class:
class User : public ActiveRecord
{
public:
User(void);
~User(void);
string name;
string lastName;
};
to create and save the user I would like to do something like:
User user = User();
user.name = "John";
user.lastName = "Smith"
user.save();
how can I get this ActiveRecord::saveRecord() method to take any object, and class definition so it writes whatever i send it:
to look like:
void ActiveRecord::saveRecord(foo_instance, FooClass){
string fileName = "data.dat";
ofstream stream(fileName.c_str(), ios::out);
if (!stream) {
cerr << "Error opening file: " << fileName << endl;
exit(1);
}
stream.write(reinterpret_cast<const char *> (foo_instance), sizeof(FooClass));
stream.close();
}
and while we're at it, what is the default Object type in c++.
eg.
in objective-c it's id
in java it's Object
in AS3 it's Object
what is it in C++??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是行不通的。
string
在堆上分配其数据(IIRC,当它大于 16 个字符时)。您的重新解释转换将不包括该堆数据。不要重新发明轮子,这是一个不平凡但已解决的问题。使用 Google Protocol Buffers、XML 或 Boost 序列化库。
您想到的是模板,但您不能“仅序列化任何对象”,因为在 POD(普通旧数据)类型之外,它们的表示并不明显。
此外,将
sizeof(BaseClass)
与 User 子类一起使用将不起作用。它会将子类的成员数据从强制转换中分割出来,而不是将其放入文件中。并且不存在“c++ 中的默认对象类型”。
This doesn't work.
string
allocates its data on the heap (IIRC, when it's larger than 16chars). Your reinterpret cast will not include that heap data.Don't reinvent the wheel, this is a non-trivial, but solved problem. Use Google Protocol Buffers, XML, or the boost serialization library.
What you have in mind is templates, but you can't "just serialize any object" because outside of POD (plain-old-data) types, their representation isn't obvious.
In addition, using
sizeof(BaseClass)
with the User subclass will not work. It'll slice the subclass's member data off of the cast and not put it in the file.And there is no "default Object type in c++".
您所提议的内容不会进行深度序列化,您通常无法知道该对象持有哪种资源。
要让它存储任何对象,您可以使用模板函数并将实际存储委托给相关类或其友元(即
operator<<()
):为此,您需要为相关类提供
operator<<()
重载:但是正如Stephen所说,为什么不使用经过验证的序列化解决方案而不是自己推出呢?
What you are proposing doesn't do a deep serialization, you can't know in general what kind of resources the object holds onto.
To let it store any object, you would use a template function and delegate the actual storing to the class in question or a friend thereof (i.e.
operator<<()
):For this to work you need to provide overloads for
operator<<()
for the classes in question:But as Stephen said, why not use proven solutions for the serialization instead of rolling your own?