In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what will happen when class encounters union in C++". In daily operation, I believe many people have doubts about how class encounters union in C++. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what will happen when class encounters union in C++". Next, please follow the editor to study!
Due to the existence of object-oriented, there is often such a class used to store attributes in the code, class A, class B, class C, class B inherits from class A, class C inherits from class B. The instances of class A, class B, class C and so on are all passed from the socket layer.
For the reusability of the code, the author adopts the following design:
Union object {
Class An a
Class B b
Class C c
}
/ / the read_objectX_from_socket function is pseudo code, and its implementation is to read out the members of a class one by one. As to why this function is implemented in this way, this is another design problem on the socket layer, leaving aside.
Void readA (union object & o)
{
Read_objectA_from_socket (o.a)
}
Void readB (union object & o)
{
ReadA (o)
Read_objectB_from_socket (o.b)
}
Void readC (union object & o)
{
ReadB (o)
Read_objectC_from_socket (o.c)
}
From the above, we can see that the author is quite confident about the memory layout of the member variables in C++ that he would think of using union to reuse the code.
Suppose the members of these three classes are defined as follows: class A {int a;}, class B: public A {int b;}, class C: public B {int c;}.
Then the memory layout in this union is actually A-V-V-A, B-V-R-V-V-R-R-C-V-L-C-L-H-L-H-L-L-C-L-L-H-L-L-C-L-C-L-L-C-L-L-D-D-C-L-D-D-C-L-D-R-D-L-D-L-D-D
The space occupied by object::an is the memory occupied by union.
The space occupied by the object::b is the memory occupied by the union by the Avatar and the BRV.
The space occupied by the object::c is the memory occupied by the union in the union, which is the amount of memory occupied by the union.
The designer skillfully makes use of the overlap feature of union and the inheritance feature of class to complete code reuse.
About when I was at school, I also liked to go to hack memory layout (not so clever, of course), and then I didn't like it very much.
Because although there is a sense of pride in writing this kind of code, but in fact, once there is bug, human beings have invented high-level language on the basis of assembly language. I think it is precisely because they feel that people need more rules to help people reduce the possibility of making mistakes, so I have been advocating writing more code that allows the compiler to check for errors.
The hole my colleagues stepped on also validates the fact that the hack memory layout is easy to make mistakes and not easy to check. For some reason of laziness, he implemented class D: public B {int d;}, class F: public C, public D {int f;}. The implementation code of readF is as follows:
Void readF (union object & o)
{
ReadC (o)
ReadD (o)
Read_objectF_from_socket (o.c)
}
In the implementation of readD, the code still seems to work, but in the implementation of readF, it is obvious that there is data reading, but the members of class F who inherit from C and D are always inexplicably messed up (of course, I didn't find this, this bug is just what I know afterwards).
If you re-look at union at this point, then the same piece of memory is occupied by both readC and readD, so the same memory is always overwritten when calling readC and DRV in readF.
If you look at the memory layout of class F, it should be a F::D::d a, b, c, d, f, that is to say, the whole readF is executed. In fact, the variable F::D::d has never been operated, so it is impossible to assign a value. Because of the random number in the stack, the variable F::D::d becomes random. Because the memory occupied by object::C::c is always operated by readC and readD at the same time. So it seems that the data are not so traceable.
It was inappropriate when I first saw this design, but I didn't find a way to achieve maximum code reuse without hack memory.
On the way back from work, I always thought that I could achieve the same goal without using hack memory, and I finally thought of it when I was nearing my place to live.
In fact, it is very simple, the reason is that on the one hand, I do not often use Clippers, on the other hand, probably read this code before, for a time preconceived, thinking did not slow down.
In fact, you only need to follow C++ 's most conventional dynamic_cast to achieve the maximum reuse of the code.
The code interface is roughly implemented as follows:
Void readA (class A * a)
{
/ / read a members
}
....
Void readD (class F * a)
{
ReadB (a)
/ / read D members
}
Because it is dynamic_cast, the compiler can help us automatically calculate the offset of each member, avoiding all kinds of errors that may occur in manual hack.
Btw, the way to use dynamic_cast can only rely on the bug that is easy to appear when the compiler discovers this kind of hack memory. When you encounter a class like class F that uses multiple inheritance mechanisms, the compiler will only report syntax errors and cannot reuse the readX function of its parent class.
At this point, the study of "what happens when class encounters union in C++" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.