更改继承顺序会产生不同的结果

时间:2021-11-09 23:01:04

I was reading Thinking in C++, and found this piece of code:

我正在阅读C ++中的Thinking,并发现了这段代码:

//: 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");
} ///:~

It produces unexpected output: Objects of class WData1 are persisted correctly, but objects of WData2 aren't. While trying to find the source of problem, and possible fix, I found out, that problem is only in reading WData2 (its stored correctly in the file). To make this code work as intended, I had to change inheritance order from:

它产生意外的输出:类WData1的对象被正确保留,但WData2的对象不是。在尝试找到问题的根源和可能的修复时,我发现,该问题仅在于读取WData2(它正确存储在文件中)。为了使这段代码按预期工作,我不得不改变继承顺序:

 WData2 : public Data, public Persistent{...

to

 WData2 :  public Persistent, public Data{...

I'm curious why inheritance order makes difference in this case. Shouldn't it make no difference?

我很好奇为什么继承顺序在这种情况下有所不同。它不应该没有区别吗?

3 个解决方案

#1


4  

The problem is that in the class Persistent you use this to point to the beginning of the memory you want to read/write. This is a problem because you use this as an inherited class. Thus unless Persistent is not the first class inherited, this will not point to the beginning of the base class:

问题是在Persistent类中,你使用它来指向你想要读/写的内存的开头。这是一个问题,因为您将其用作继承类。因此,除非Persistent不是继承的第一个类,否则这不会指向基类的开头:

This is what we have for Wdata1 and Wdata2 (padding and alignment ignored):

这就是我们对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       ->|

#2


2  

The problem is in the calls in Persistent:

问题在于Persistent中的调用:

out.write((char*)this, objSize);
in.read((char*)this, objSize);

Here, this is a Persistent* and it is assumed to point to the beginning of the full object. That is only true if Persistent is the very first base class.

这里,这是一个Persistent *,它被假定为指向完整对象的开头。只有Persistent是第一个基类才是真的。

To work everywhere you can do this little known trick:

要到处工作,你可以做这个鲜为人知的技巧:

void *that = dynamic_cast<void*>(this);
out.write((char*)that, objSize);
in.read((char*)that, objSize);

The dynamic_cast<void*>() guarantees that you'll have the pointer to the most derived object.

dynamic_cast ()保证您将拥有指向派生程度最高的对象的指针。

WARNING!: Alas! This will not work unless the type is polymorphic. And the Persistent trick will not work with polymorphic types. So this answer, as it is, is useless. I am not deleting it because it may still be interesting why you shouldn't do this.

警告!:唉!除非类型是多态的,否则这将不起作用。并且Persistent技巧不适用于多态类型。所以这个答案实际上是无用的。我不是删除它,因为它可能仍然有趣为什么你不应该这样做。

#3


0  

The cause of your problem might be cause your base-class initialization in the WData2 constructor initialization list. You need to call the base constructors in the same order you inherit the base classes.

您的问题的原因可能是在WData2构造函数初始化列表中导致基类初始化。您需要以继承基类的相同顺序调用基础构造函数。

So change

WData2(...) : Data(...), Persistent(...) { ... }

to

WData2(...) : Persistent(...), Data(...) { ... }

#1


4  

The problem is that in the class Persistent you use this to point to the beginning of the memory you want to read/write. This is a problem because you use this as an inherited class. Thus unless Persistent is not the first class inherited, this will not point to the beginning of the base class:

问题是在Persistent类中,你使用它来指向你想要读/写的内存的开头。这是一个问题,因为您将其用作继承类。因此,除非Persistent不是继承的第一个类,否则这不会指向基类的开头:

This is what we have for Wdata1 and Wdata2 (padding and alignment ignored):

这就是我们对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       ->|

#2


2  

The problem is in the calls in Persistent:

问题在于Persistent中的调用:

out.write((char*)this, objSize);
in.read((char*)this, objSize);

Here, this is a Persistent* and it is assumed to point to the beginning of the full object. That is only true if Persistent is the very first base class.

这里,这是一个Persistent *,它被假定为指向完整对象的开头。只有Persistent是第一个基类才是真的。

To work everywhere you can do this little known trick:

要到处工作,你可以做这个鲜为人知的技巧:

void *that = dynamic_cast<void*>(this);
out.write((char*)that, objSize);
in.read((char*)that, objSize);

The dynamic_cast<void*>() guarantees that you'll have the pointer to the most derived object.

dynamic_cast ()保证您将拥有指向派生程度最高的对象的指针。

WARNING!: Alas! This will not work unless the type is polymorphic. And the Persistent trick will not work with polymorphic types. So this answer, as it is, is useless. I am not deleting it because it may still be interesting why you shouldn't do this.

警告!:唉!除非类型是多态的,否则这将不起作用。并且Persistent技巧不适用于多态类型。所以这个答案实际上是无用的。我不是删除它,因为它可能仍然有趣为什么你不应该这样做。

#3


0  

The cause of your problem might be cause your base-class initialization in the WData2 constructor initialization list. You need to call the base constructors in the same order you inherit the base classes.

您的问题的原因可能是在WData2构造函数初始化列表中导致基类初始化。您需要以继承基类的相同顺序调用基础构造函数。

So change

WData2(...) : Data(...), Persistent(...) { ... }

to

WData2(...) : Persistent(...), Data(...) { ... }