What is the data structure of C++ class?
how it works in assembly level?
IF statement is the compare + conditional jump of code line.
Array and string is the chain link of datas.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The compiler assigns offsets to all members, and includes these in all load/store operations on members:
becomes (ARM assembler code used for simplicity):
As
structrespectiveclasslayout does not change after compilation, the 4 is hardcoded into the instruction stream here.Creating an instance works by allocating memory, and passing the pointer from the allocation function to everyone expecting a pointer to the struct:
If there is a constructor
We end up with a slightly more complicated way to create objects (which you should be able to figure out without comments):
The
get_bazimplementation is the same as for thefooclass.Now if I construct such an object and get the baz value:
I end up with
r0containing the value7.For
virtualmethods, a hidden data member is created, which is a pointer to a table of functions:becomes
Calling this function is done indirectly:
Here,
r0is now5, because that is what the constructor stored there.