Address: http://hi.baidu.com/chzhao007/blog/item/e9dbc5ac1191bc034b36d6df.html
A few days ago, I was underestimated by a bird. I admit thatCodeSo I have to review it here: All the code I posted isNot my final code!
I wrote a lot of code using C in my blog, but it does not mean that I do not advocate C ++'s object programming, I am only engaged in many aspects of C and compilation is more convenient, so using C to write stuff is easier than C ++. In order to make my blog more sophisticated, I would like to write down my essential understanding of the C ++ class here. Review here: my blog does not represent my current real level. Everyone needs a bit of housekeeping skills. Otherwise, it is really difficult to mix in this society. As a housekeeping skill, it is generally not leaked.
I will not discuss in theory what C ++ class is (which class represents a car, ducks won't find it here ), here, we will only look at how classes are implemented and run at the machine code layer.
Here I will first define a test class
Class One {
Public:
One (): A (10 ){}
~ One (){}
Void Seta (int A) {This-> A = ;}
Int geta (void) {return ;}
Void SETB (int B) {This-> B = B ;}
Int getb (void) {return B ;}
PRIVATE:
Int A, B;
};
Next Let's define a class element.
One testone;
Next let's take a look at the memory size of the testone defined.
STD: cout <sizeof (testone) <"\ n"; // after running, you will find that testone has only 8 bytes in the memory, which is the size of two int values.
Here we can assume that one is equivalent
Struct {
Int;
Int B;
} One;
If you really want to be handsome, you can dynamically declare a class test * One = (one *) New int (2 );
Now let's define another one element One testtwo; similarly, testtwo is 8 bytes in size. Now we have a question: how does testone and testtwo have the same size but not the same entity ()~ Is one () Seta () geta () and other functions linked together?
Now let's look at it at the Assembly level.
Testone. Seta (1 );
00401098 6a 01 Push 1
0040109a 8d 4D EC Lea ECx, [ebp-14h]
0040366d E8 72 FF call @ ILT + 15 (one: Seta) (00401014)
Testtwo. Seta (2 );
004010a2 6a 02 push 2
004010a4 8d 4D E4 Lea ECx, [ebp-1Ch]
004010a7 E8 68 FF call @ ILT + 15 (one: Seta) (00401014)
We found that testone and testtwo use the same address 00401014 when calling their respective Seta. Here we can be sure that the elements defined by one of the same class share the method functions defined by one.
We can infer that the difference between testone and testtwo is that the addresses of the corresponding int A \ int B variables are different. In the assembly code, we found that the difference between EBP-14h and EBP-1ch is exactly 8 bytes. My code
One testone;
One testtwo;
The defined testone and testtwo are tied, so testtwo follows testone in memory splitting.
Here we can summarize the implementation of the c ++ class, the compiler will
Structure struct {
Int;
Int B;
} One;
And
A series of Function Methods
One (): A (10 ){}
~ One (){}
Void Seta (int A) {This-> A = ;}
Int geta (void) {return ;}
Void SETB (int B) {This-> B = B ;}
Int getb (void) {return B ;}
Bound automatically. This is like a production line. The production equipment is fixed, but the incoming and outgoing parts are being replaced. These parts are a set of parts that match the production line after being processed (declared) in advance.
I have briefly described the nature of the C ++ class. I hope you can use it easily.C LanguageImplement the C ++ class (as long as the structure and method are added to the solidification ).
I am here to say a few more words, kung fu experts are all soldiers, everything is blade. The focus of programming is not to stick to the language you have learned, but to learn NLP.
End