網(wǎng)站中英文切換代碼線上銷售方案
序列化和反序列化
實(shí)現(xiàn)思路:
- 序列化:將對(duì)象的狀態(tài)信息轉(zhuǎn)換為可以存儲(chǔ)或傳輸?shù)母袷?#xff0c;通常是字節(jié)流。
- 確定要序列化的對(duì)象的數(shù)據(jù)成員。
- 將這些數(shù)據(jù)成員按照一定的規(guī)則(如二進(jìn)制、文本、JSON、XML 等)編碼為字節(jié)序列。
- 將生成的字節(jié)序列存儲(chǔ)到文件或發(fā)送到網(wǎng)絡(luò)。
- 反序列化:將字節(jié)流恢復(fù)為對(duì)象的狀態(tài)信息。
- 從文件或網(wǎng)絡(luò)接收字節(jié)序列。
- 按照序列化時(shí)使用的規(guī)則解碼字節(jié)序列。
- 根據(jù)解碼后的數(shù)據(jù)成員創(chuàng)建或更新對(duì)象。
#include <iostream>
#include <fstream>
#include <string>class SerializableObject {
private:int data1;double data2;std::string data3;public:SerializableObject(int d1 = 0, double d2 = 0.0, const std::string& d3 = "") : data1(d1), data2(d2), data3(d3) {}// 序列化函數(shù)void serialize(const std::string& filename) const {std::ofstream file(filename, std::ios::binary);if (file.is_open()) {// 寫入數(shù)據(jù)成員file.write(reinterpret_cast<const char*>(&data1), sizeof(data1));file.write(reinterpret_cast<const char*>(&data2), sizeof(data2));// 先寫入字符串長(zhǎng)度size_t len = data3.length();file.write(reinterpret_cast<const char*>(&len), sizeof(len));// 再寫入字符串內(nèi)容file.write(data3.c_str(), len);file.close();} else {std::cerr << "Failed to open file for serialization." << std::endl;}}// 反序列化函數(shù)void deserialize(const std::string& filename) {std::ifstream file(filename, std::ios::binary);if (file.is_open()) {// 讀取數(shù)據(jù)成員file.read(reinterpret_cast<char*>(&data1), sizeof(data1));file.read(reinterpret_cast<char*>(&data2), sizeof(data2));// 先讀取字符串長(zhǎng)度size_t len;file.read(reinterpret_cast<char*>(&len), sizeof(len));// 再讀取字符串內(nèi)容data3.resize(len);file.read(&data3[0], len);file.close();} else {std::cerr << "Failed to open file for deserialization." << std::endl;}}void display() const {std::cout << "data1: " << data1 << ", data2: " << data2 << ", data3: " << data3 << std::endl;}
};int main() {SerializableObject obj(42, 3.14, "Hello, World!");std::string filename = "object.bin";// 序列化obj.serialize(filename);std::cout << "Serialized object: " << std::endl;obj.display();SerializableObject newObj;// 反序列化newObj.deserialize(filename);std::cout << "Deserialized object: " << std::endl;newObj.display();return 0;
}
代碼解釋:
SerializableObject
?類包含三個(gè)數(shù)據(jù)成員:data1
(整數(shù))、data2
(雙精度浮點(diǎn)數(shù))和?data3
(字符串)。serialize
?函數(shù):- 使用?
std::ofstream
?以二進(jìn)制模式打開(kāi)文件。 - 對(duì)于?
data1
?和?data2
,使用?file.write
?將它們的二進(jìn)制表示寫入文件。 - 對(duì)于?
data3
,先寫入字符串的長(zhǎng)度,再寫入字符串的內(nèi)容。
- 使用?
deserialize
?函數(shù):- 使用?
std::ifstream
?以二進(jìn)制模式打開(kāi)文件。 - 對(duì)于?
data1
?和?data2
,使用?file.read
?讀取它們的二進(jìn)制表示。 - 對(duì)于?
data3
,先讀取字符串的長(zhǎng)度,再讀取字符串的內(nèi)容。
- 使用?
display
?函數(shù):打印對(duì)象的數(shù)據(jù)成員。
?如何處理對(duì)象的生命周期管理?
實(shí)現(xiàn)思路:
- 構(gòu)造函數(shù):用于對(duì)象的初始化,可進(jìn)行資源分配和成員初始化。
- 析構(gòu)函數(shù):用于對(duì)象銷毀時(shí)釋放資源,如釋放動(dòng)態(tài)分配的內(nèi)存、關(guān)閉文件等。
- 拷貝構(gòu)造函數(shù)和拷貝賦值運(yùn)算符:控制對(duì)象的拷貝行為,避免淺拷貝導(dǎo)致的資源問(wèn)題。
- 移動(dòng)構(gòu)造函數(shù)和移動(dòng)賦值運(yùn)算符:實(shí)現(xiàn)對(duì)象資源的高效移動(dòng),避免不必要的拷貝。
#include <iostream>
#include <string>
#include <utility>class ResourceManagingObject {
private:int* data;size_t size;public:// 構(gòu)造函數(shù)ResourceManagingObject(size_t s = 0) : size(s) {if (s > 0) {data = new int[s];for (size_t i = 0; i < s; ++i) {data[i] = i;}} else {data = nullptr;}}// 析構(gòu)函數(shù)~ResourceManagingObject() {delete[] data;}// 拷貝構(gòu)造函數(shù)ResourceManagingObject(const ResourceManagingObject& other) : size(other.size) {if (other.data) {data = new int[size];for (size_t i = 0; i < size; ++i) {data[i] = other.data[i];}} else {data = nullptr;}}// 拷貝賦值運(yùn)算符ResourceManagingObject& operator=(const ResourceManagingObject& other) {if (this == &other) return *this;delete[] data;size = other.size;if (other.data) {data = new int[size];for (size_t i = 0; i < size; ++i) {data[i] = other.data[i];}} else {data = nullptr;}return *this;}// 移動(dòng)構(gòu)造函數(shù)ResourceManagingObject(ResourceManagingObject&& other) noexcept : data(other.data), size(other.size) {other.data = nullptr;other.size = 0;}// 移動(dòng)賦值運(yùn)算符ResourceManagingObject& operator=(ResourceManagingObject&& other) noexcept {if (this == &other) return *this;delete[] data;data = other.data;size = other.size;other.data = nullptr;other.size = 0;return *this;}void display() const {if (data) {for (size_t i = 0; i < size; ++i) {std::cout << data[i] << " ";}std::cout << std::endl;} else {std::cout << "No data." << std::endl;}}
};int main() {ResourceManagingObject obj1(5);std::cout << "Original object: ";obj1.display();// 拷貝構(gòu)造ResourceManagingObject obj2 = obj1;std::cout << "Copied object: ";obj2.display();// 移動(dòng)構(gòu)造ResourceManagingObject obj3 = std::move(obj1);std::cout << "Moved object: ";obj3.display();std::cout << "Original object after move: ";obj1.display();return 0;
}
代碼解釋:
ResourceManagingObject
?類管理一個(gè)動(dòng)態(tài)分配的整數(shù)數(shù)組。- 構(gòu)造函數(shù):根據(jù)大小分配內(nèi)存并初始化數(shù)組元素。
- 析構(gòu)函數(shù):釋放動(dòng)態(tài)分配的內(nèi)存。
- 拷貝構(gòu)造函數(shù)和拷貝賦值運(yùn)算符:深拷貝資源,避免淺拷貝導(dǎo)致的資源共享和潛在的內(nèi)存問(wèn)題。
- 移動(dòng)構(gòu)造函數(shù)和移動(dòng)賦值運(yùn)算符:將資源從源對(duì)象移動(dòng)到目標(biāo)對(duì)象,源對(duì)象放棄資源所有權(quán)。
?
?