[英]Changing inheritance order produces different results
我正在閱讀C ++中的Thought,發現了這段代碼:
//: C06:Persist1.cpp
// Simple persistence with MI
#include <iostream>
#include <fstream>
using namespace std;
class Persistent {
int objSize; // Size of stored object
public:
Persistent(int sz) : objSize(sz) {}
void write(ostream& out) const {
out.write((char*)this, objSize);
}
void read(istream& in) {
in.read((char*)this, objSize);
}
};
class Data {
float f[3];
public:
Data(float f0 = 0.0, float f1 = 0.0,
float f2 = 0.0) {
f[0] = f0;
f[1] = f1;
f[2] = f2;
}
void print(const char* msg = "") const {
if(*msg) cout << msg << " ";
for(int i = 0; i < 3; i++)
cout << "f[" << i << "] = "
<< f[i] << endl;
}
};
class WData1 : public Persistent, public Data {
public:
WData1(float f0 = 0.0, float f1 = 0.0,
float f2 = 0.0) : Data(f0, f1, f2),
Persistent(sizeof(WData1)) {
cout<<"size of w1 "<<sizeof(WData1)<<endl;
}
};
class WData2 : public Data, public Persistent {
public:
WData2(float f0 = 0.0, float f1 = 0.0,
float f2 = 0.0) : Data(f0, f1, f2),
Persistent(sizeof(WData2)) {
cout<<"size of w2 "<<sizeof(WData2)<<endl;
}
};
int main() {
{
ofstream f1("f1.dat"), f2("f2.dat"),f3("f3.dat"), f4("f4.dat");
WData1 d1(1.1, 2.2, 3.3);
WData2 d2(4.4, 5.5, 6.6);
WData1 d3(1.1, 2.2, 3.3);
WData2 d4(4.4, 5.5, 6.6);
d1.print("d1 before storage");
d2.print("d2 before storage");
d3.print("d3 before storage");
d4.print("d4 before storage");
d1.write(f1);
d2.write(f2);
d3.write(f3);
d4.write(f4);
} // Closes files
ifstream f1("f1.dat"), f2("f2.dat"),f3("f3.dat"), f4("f4.dat");
WData1 d1;
WData2 d2;
WData1 d3;
WData2 d4;
d1.read(f1);
d2.read(f2);
d3.read(f3);
d4.read(f4);
d1.print("d1 before storage");
d2.print("d2 before storage");
d3.print("d3 before storage");
d4.print("d4 before storage");
} ///:~
它會產生意外的輸出:WData1類的對象可以正確持久保存,而WData2的對象則不能正確保存。 在嘗試查找問題的根源和可能的解決方法時,我發現,該問題僅在於讀取WData2(其已正確存儲在文件中)。 為了使此代碼按預期工作,我必須將繼承順序更改為:
WData2 : public Data, public Persistent{...
至
WData2 : public Persistent, public Data{...
我很好奇為什么在這種情況下繼承順序會有所不同。 它不應該沒有區別嗎?
問題是在Persistent類中,您可以使用this
指向要read/write
的內存的開頭。 這是一個問題,因為您將此用作繼承的類。 因此,除非Persistent
是不繼承了第一類, this
將不會指向基類的開頭:
這就是Wdata1
和Wdata2
(填充和對齊方式被忽略):
Wdata1
+-----------+----------------------+
| objSize | f[0] f[1] f[2] |
+-----------+----------------------+
|Persistent | Data |
^
|
this (of Persistent)
|<- what you read/ write ->|
WData2
+----------------------+-----------+
| f[0] f[1] f[2] | objSize |
+----------------------+-----------+
| Data | Persistent|
^
|
this (of Persistent)
|<- what you read/ write ->|
問題出在Persistent
:
out.write((char*)this, objSize);
in.read((char*)this, objSize);
在此, this
是一個Persistent*
,並假定指向完整對象的開頭。 僅當Persistent
是第一個基類時才如此。
要在任何地方工作,您可以執行以下鮮為人知的技巧:
void *that = dynamic_cast<void*>(this);
out.write((char*)that, objSize);
in.read((char*)that, objSize);
dynamic_cast<void*>()
確保您將擁有指向最派生對象的指針。
警告! :A! 除非類型是多態的,否則這將不起作用。 而Persistent
技巧將不適用於多態類型。 因此,這個答案是沒有用的。 我不會刪除它,因為為什么您不應該這樣做仍然很有趣。
問題的原因可能是導致WData2
構造函數初始化列表中的基類初始化。 您需要以繼承基類的相同順序調用基構造函數。
所以改變
WData2(...) : Data(...), Persistent(...) { ... }
至
WData2(...) : Persistent(...), Data(...) { ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.