java.io.Serializable的C/C++等价是什么?
java.io.Serializable的C/C++等价是什么?
有关序列化库的引用: > Serialize Data Structures in C 还有: > http://troydhanson.github.io/tpl/index.html 但是这样的等价性是否存在呢? 所以如果我在Java中有一个抽象类,C/C++中的可序列化类如何? import java.io.Serializable; public interface SuperMan extends Serializable{ /** * Count the number of abilities. * @return */ public int countAbility(); /** * Get the ability with index k. * @param k * @return */ public long getAbility(int k); /** * Get the array of ability from his hand. * @param k * @return */ public int[] getAbilityFromHand(int k); /** * Get the finger of the hand. * @param k * @return */ public int[][] getAbilityFromFinger(int k); //check whether the finger with index k is removed. public boolean hasFingerRemoved(int k); /** * Remove the finger with index k. * @param k */ public void removeFinger(int k); } 任何可序列化的C/C++对象可以像Java一样继承吗? 解决方法
没有标准的库类以与Java相同的方式实现序列化.有一些库可以促进序列化,但是对于基本需求,通常可以通过重载这样的插入和提取操作来使您的类可序列化:
class MyType { int value; double factor; std::string type; public: MyType() : value(0),factor(0.0),type("none") {} MyType(int value,double factor,const std::string& type) : value(value),factor(factor),type(type) {} // Serialized output friend std::ostream& operator<<(std::ostream& os,const MyType& m) { return os << m.value << ' ' << m.factor << ' ' << m.type; } // Serialized input friend std::istream& operator>>(std::istream& is,MyType& m) { return is >> m.value >> m.factor >> m.type; } }; int main() { std::vector<MyType> v {{1,2.7,"one"},{4,5.1,"two"},{3,0.6,"three"}}; std::cout << "Serialize to standard output." << 'n'; for(auto const& m: v) std::cout << m << 'n'; std::cout << "nSerialize to a string." << 'n'; std::stringstream ss; for(auto const& m: v) ss << m << 'n'; std::cout << ss.str() << 'n'; std::cout << "Deserialize from a string." << 'n'; std::vector<MyType> v2; MyType m; while(ss >> m) v2.push_back(m); for(auto const& m: v2) std::cout << m << 'n'; } 输出: Serialize to standard output. 1 2.7 one 4 5.1 two 3 0.6 three Serialize to a string. 1 2.7 one 4 5.1 two 3 0.6 three Deserialize from a string. 1 2.7 one 4 5.1 two 3 0.6 three 序列化格式完全取决于程序员,您有责任确保要序列化的类的每个成员本身都是可序列化的(已定义插入/提取操作).您还必须处理字段如何分隔(空格或新行或零终止?). 所有的基本类型都有预定义的序列化(插入/提取)运算符,但是您仍然需要注意可以包含(例如)空格或新行的std :: string等(如果使用空格或新行)行作为您的字段分隔符). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- java – 列出不解析强制转换的实现
- Servlet多文件打包下载
- java – 可以将BiFunction引用传递给期望功能接口的方法吗?
- java – JUnit 5 – 使用JUnit Jupiter引擎时IntelliJ IDEA
- Java 面向对象之static,final,匿名对象,内部类,包,修饰符
- java – android spinner NullPointerException
- java – Arquillian与Glassfish V4
- java – 如果需要更低延迟的代码,我们为什么要选择微服务呢
- Java GUI编程:设置Fore / Background
- java – RandomAccessFileOrArray的替代方法(byte [])