虚拟成员(结构)之类的东西(something like virtual member(structure))

编程入门 行业动态 更新时间:2024-10-23 09:21:01
虚拟成员(结构)之类的东西(something like virtual member(structure))

我有A级基本成员和职能:

class A{ public: typedef struct{ int aa; int bb; } stEntry; stEntry entry; void function1(); void function2(); };

比B类更应该扩展A类包括结构stEntry ...

class B : public A{ public: typedef struct :stEntry { int cc; } stEntry; stEntry entry; void function3(); };

接着:

int main() { A a; B b; a.entry.aa = 1; b.entry.aa = 2; b.entry.cc = 3; cout << "class A:"<<sizeof(a)<< endl; cout << "class B:"<<sizeof(b)<< endl; return 0; }

我明白了

class A:8 class B:20

所以B类包含2个实例--8个字节(A类成员)+ 12个字节(B类成员)。

有没有办法如何扩展B类的结构stEntry? (没有2个实例)

I have class A with basic members and functions:

class A{ public: typedef struct{ int aa; int bb; } stEntry; stEntry entry; void function1(); void function2(); };

Than class B that should extend class A including structure stEntry...

class B : public A{ public: typedef struct :stEntry { int cc; } stEntry; stEntry entry; void function3(); };

and then:

int main() { A a; B b; a.entry.aa = 1; b.entry.aa = 2; b.entry.cc = 3; cout << "class A:"<<sizeof(a)<< endl; cout << "class B:"<<sizeof(b)<< endl; return 0; }

I get

class A:8 class B:20

So class B contains 2 instances - 8 bytes(A class member) + 12 bytes(B class member).

Is there a way how to extend structure stEntry for class B? (without have 2 instances)

最满意答案

具有虚拟继承的排序:

struct stEntryBase { int aa; int bb; }; struct A : virtual stEntryBase { typedef stEntryBase stEntry; void function1(); void function2(); }; struct stEntryDerived : virtual stEntryBase { int cc; }; struct B : A, stEntryDerived { typedef stEntryDerived stEntry; void function3(); };

如果你想进入另一个级别的继承,那么B将从stEntryDerived派生出来。

现在你必须将字段称为a.aa , b.cc ,没有成员entry 。 此外, stEntry类型不再是POD(因此bb和cc可能不再在内存中相邻)。 最后,由于虚拟继承而导致的大小增加实际上可能大于两个int 。

您可以做的是从B的实例获取stEntryDerived*或stEntryDerived& 。 然后,该指针/引用可用于访问aa , bb和cc作为stEntryDerived的成员,而无需用户了解B 所以你已经实现了界面分离,但需要付出一些代价。

Sort of, with virtual inheritance:

struct stEntryBase { int aa; int bb; }; struct A : virtual stEntryBase { typedef stEntryBase stEntry; void function1(); void function2(); }; struct stEntryDerived : virtual stEntryBase { int cc; }; struct B : A, stEntryDerived { typedef stEntryDerived stEntry; void function3(); };

If you want to go another level of inheritance then B would derived virtually from stEntryDerived.

Now you have to refer to the fields as a.aa, b.cc, there is no member entry. Also, the stEntry types are no longer POD (so bb and cc may no longer be adjacent in memory). Finally, the size increase due to the virtual inheritance might actually be bigger than two ints.

What you can do, is get an stEntryDerived* or stEntryDerived& from an instance of B. Then that pointer/reference can be used to access aa, bb, and cc as the members of stEntryDerived, without the user needing to know about B. So you've achieved separation of interface, at some cost.

更多推荐

本文发布于:2023-08-04 16:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417694.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:成员   东西   结构   structure   virtual

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!