基础和派生的类内存布局,带有void*成员

发布于 2025-02-02 07:09:49 字数 1999 浏览 2 评论 0原文

我解决了自己的问题,但是需要帮助理解为什么。客户可以访问我可以插入数据的基类,b,我刚刚传递,永远不会使用。客户将具有getter功能,可以随时获得数据。当我使用“方法1”:base*派生对象时,我得到了我想要的东西。

我想要的:客户端的数据b以其原始形式回到其原始形式,如果您只是从worde stype*中施放到void*,然后再次返回到wory stype*。当我使用“方法2”:设置派生的* derived 对象,然后客户的数据检索b dim not 以其原始形式返回,相反,我得到了垃圾值,因此显然不是内存中正确位置与用户数据的正确位置。

我最初尝试了两个方法,但是找出方法了,我想让我想要什么。我想了解为什么,我会认为base* to 派生对象的内存布局与derived* to to <<代码>派生对象,只是...“切成薄片”。

以下是一个人为说明我的意思,不一定是工作代码的示例。

struct Base { 
    int a;
    void* b;
};

class Example {
  struct Derived : public Base {
    Example* c;
  };
  Base* data1;  // Method 1: Gives me what I want
  Derived* data2;  // Method 2: Does not give me what I want

  public:
    // Method 1: Gives me what I want
    void SetData1(Base* foo) {
        data1 = new Derived;
        data1->a = foo->a;
        data1->b = foo->b;
        static_cast<Derived*>(data1)->c = this;
    }
    // Method 2: Does not give me what I want
    void SetData2(Base* foo) {
        data2 = new Derived;
        data2->a = foo->a;
        data2->b = foo->b;
        data2->c = this;
    }

    // Getter function doesn't make a difference here
    // void* GetData() {
        // return data1->b; //Method 1
        // return data2->b; //Method 2
    // }
};

int main() {
  // Example use-case
     Base* foo = new Base;
     foo->a = 2;
     SomeUserData* bar = new SomeUserData;
     foo->b = bar;
     
     Example myexample;
     // Method 1:   :)
     myexample.SetData1(foo); 
     SomeUserData* test1 = static_cast<SomeUserData>myexample.GetData();
     // Method 2:   :(
     myexample.SetData2(foo);
     SomeUserData* test2 = static_cast<SomeUserData>myexample.GetData();     
};

I solved my own problem, but need help understanding the why. Client has access to Base class that they can insert their data whatever it is, b, that I just pass around, never use. Client will have a getter function to get their data back whenever they want. When I use "Method 1": a Base* to Derived object, I get what I want.

What I want: the client's data b back in its original form, which I think is guaranteed by standards if you simply cast from SomeType* to void* and then back to SomeType* again. When I use "Method 2": setting a Derived* to Derived object, then the client's data when they retrieve b does not come back in its original form, instead I get garbage values, so clearly not the right location in memory to the user's data.

I originally tried Method two, but figured out Method one gets me what I want. I'd like to understand why, I would have thought the Base* to Derived object would have a similar memory layout as Derived* to Derived object, just... "sliced".

Below is a contrived example to illustrate what I mean, not necessarily working code.

struct Base { 
    int a;
    void* b;
};

class Example {
  struct Derived : public Base {
    Example* c;
  };
  Base* data1;  // Method 1: Gives me what I want
  Derived* data2;  // Method 2: Does not give me what I want

  public:
    // Method 1: Gives me what I want
    void SetData1(Base* foo) {
        data1 = new Derived;
        data1->a = foo->a;
        data1->b = foo->b;
        static_cast<Derived*>(data1)->c = this;
    }
    // Method 2: Does not give me what I want
    void SetData2(Base* foo) {
        data2 = new Derived;
        data2->a = foo->a;
        data2->b = foo->b;
        data2->c = this;
    }

    // Getter function doesn't make a difference here
    // void* GetData() {
        // return data1->b; //Method 1
        // return data2->b; //Method 2
    // }
};

int main() {
  // Example use-case
     Base* foo = new Base;
     foo->a = 2;
     SomeUserData* bar = new SomeUserData;
     foo->b = bar;
     
     Example myexample;
     // Method 1:   :)
     myexample.SetData1(foo); 
     SomeUserData* test1 = static_cast<SomeUserData>myexample.GetData();
     // Method 2:   :(
     myexample.SetData2(foo);
     SomeUserData* test2 = static_cast<SomeUserData>myexample.GetData();     
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文