Looking at C ++ from the perspective of Assembly (the arithmetic operator overload trap) 23

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

In Arithmetic Operator overloading, the "=" overload may be the most frequently used one. However, many people mistakenly think that in a function, where "=" occurs in a class, it is to call the arithmetic operator to overload it. In fact, this is not the case. Why? Let's take a look at the followingCode. First, we define a basic class:

[CPP] View plaincopy
  1. ClassData
  2. {
  3. Char* Value;
  4. IntNumber;
  5. Public:
  6. ExplicitData (IntNum = 0 ){
  7. If(Num ){
  8. Number = num;
  9. Value = (Char*) Malloc (Num );
  10. }
  11. }
  12. Data (ConstData & D ){
  13. Number = D. get_number ();
  14. Value = (Char*) Malloc (D. get_number ());
  15. Memmove (value, D. get_point (), D. get_number ());
  16. }
  17. ~ Data (){
  18. If(Number)
  19. Free (value );
  20. }
  21. Data & operator = (ConstData & D ){
  22. If(Number)
  23. Free (value );
  24. Number = D. get_number ();
  25. Value = (Char*) Malloc (D. get_number ());
  26. Memmove (value, D. get_point (), D. get_number ());
  27. Return*This;
  28. }
  29. IntGet_number ()Const{ReturnNumber ;}
  30. Char* Get_point ()Const{ReturnValue ;}
  31. };

After the function is defined, we start to call this class. The same Code is as follows:

[CPP] View plaincopy
  1. 45: Data M (10 );
  2. 0040108d push 0ah
  3. 0040366f Lea ECx, [ebp-14h]
  4. 00401092 call @ ILT + 30 (data: Data) (00401023)
  5. 00401097 mov dword ptr [ebp-4], 0
  6. 46: Data P = m;
  7. 0040da-e Lea eax, [ebp-14h]
  8. 004010a1 push eax
  9. 004010a2 Lea ECx, [ebp-1Ch]
  10. 004010a5 call @ ILT + 35 (data: Data) (00401028)
  11. 004010aa mov byte PTR [ebp-4], 1
  12. 47: P = m;
  13. 004010ae Lea ECx, [ebp-14h]
  14. 004010b1 push ECx
  15. 004010b2 Lea ECx, [ebp-1Ch]
  16. 004010b5 call @ ILT + 5 (data: Operator =) (0040100a)
  17. 48 :}

There are three sentences above. We will analyze them one by one:

A temporary variable is defined to call the data constructor.

46: A Temporary Variable P is displayed. It is found that the data class does not call the arithmetic operator to overload the function, but calls the data constructor, as shown in 45 sentences, the called constructor is definitely not a common constructor, so the rest of the results can only be a copy of the constructor.

47: The Code of 46 sentences is the same, but the called function is the arithmetic operator to overload the function.

Therefore, where "=" occurs, it is not always an arithmetic operator overload function, or a copy constructor. So when is the copy constructor and when is the arithmetic operator reload function? The judgment criteria are actually very simple. If the temporary variable appears for the first time, only the copy constructor can be called. If the variable already exists, just like 47 sentences, only the arithmetic operator overload function can be called, however, the arithmetic operator overload function we defined here has a trap. Do you know it?

I would like to remind you that the overload of Arithmetic Operators here does not need to judge whether the copied operators are themselves?

[CPP] View plaincopy
    1. VoidProcess ()
    2. {
    3. Data M (10 );
    4. Data P = m;
    5. P = P;
    6. }

In the last sentence, if the arithmetic operator can be copied to itself, the code can be compiled and run normally, but in some cases many unexpected situations may occur. You can follow my ideas:

[CPP] View plaincopy
  1. Data & operator = (ConstData & D ){
  2. If(This==& D)/* Check whether it is self-copy action */
  3. Return*This;
  4. If(Number)
  5. Free (value );
  6. Number = D. get_number ();
  7. Value = (Char*) Malloc (D. get_number ());
  8. Memmove (value, D. get_point (), D. get_number ());
  9. Return*This;
  10. }

If the code above does not determine whether the Copied object is itself, we find that the data of the actually value is actually free. In this case, the memory is re-allocated, and the copied data only knows what the data is. There is a lot of uncertainty in the memory space that the original value points to, which is the trap of overloading Arithmetic Operators.

 

[Note: since the end of the C ++ series with compilation, we will start the data structure andAlgorithmFor more information, see]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.