Return only functions of different types, how to implement overloading in C + +?

Source: Internet
Author: User

C + + supports function overloading, so-called overloads are within the same namespace, function names are the same, functions with different parameters (number of arguments or different parameter types) can coexist. However, if the parameters and function names are the same, the compiler will not overload the error. But in reality, sometimes it just needs to return a different type of function, and in this case C + + does not support overloading, such as:

[CPP]View PlainCopy
  1. typedef struct Tdata {
  2. int A;
  3. int b;
  4. } Tdata;
  5. Class CTest {
  6. Public
  7. Tdata &getdata ()
  8. {
  9. return data;
  10. }
  11. Const Tdata &getdata ()
  12. {
  13. return data;
  14. }
  15. Private
  16. Tdata data;
  17. };

When you need to read data only, call the const tdata &getdata (), call Tdata &getdata () when the data needs to be changed, and now because C + + does not support this situation, then we can only take a compromise, the first method is, Tdata &getdata () is used in both reading and writing, which destroys the program's original intent in places where only the reading is needed; the second approach is to call the Const Tdata &getdata () in a read-only place, so that, where it needs to be written, You have to force type conversions, such as:

[HTML]View PlainCopy
    1. CTest test;
    2. Tdata *pData = (Tdata *) &test.getdata ();

As above, it is better to convert the data into the form of a pointer so that it can be modified in a way that is relatively the first practice. So is there a way to Tdata &getdata () and const tdata &getdata (), like a function overload, to coexist at the same time? The answer is yes, here's a way to bypass the compiler's limitations, such as:

[HTML]View PlainCopy
    1. #define  GET_DATA ()  \  
    2.      public:\  
    3.     tdata &getdata ()  {  return data; } \  
    4.     const TData & GetData ()  const { return data; }  
    5.   
    6. typedef struct tdata {  
    7.     int a;   
    8.     int b;  
    9. } tdata;   
    10.   
    11. CLASS CTEST {  
    12.      get_data ()   
    13. PRIVATE:  
    14.     tdata  data;  
    15. };  

As above through the way of macro definition, so that you can bypass the compiler check, to achieve the coexistence of two situations, the following look at the use of examples, as follows:

[HTML]View PlainCopy
  1. int main (int argc, char *argv[])
  2. {
  3. Qapplication app (argc, argv);
  4. CTest test;
  5. /* Write */
  6. Tdata &data = test.getdata ();
  7. data.a = 1;
  8. data.b = 2;
  9. /* Read */
  10. Const Tdata &data1 = test.getdata ();
  11. Qdebug () << data1.a << data1.b;
  12. return App.exec ();
  13. }

Isn't it cool, then let's see if writing and reading are really two functions, or a function that looks at the disassembly of the above code, as follows:

[HTML]View PlainCopy
  1. CTest test;
  2. Tdata &data = test.getdata ();
  3. 0x004013e5 <+43;: lea-0x24 (%EBP),%eax
  4. 0x004013e8 <+46;: mov%eax, (%ESP)
  5. 0x004013eb <+49: Call 0x406440 <_zn5ctest7getdataev>
  6. 0x004013f0 <+54;: mov%eax,-0x10 (%EBP)
  7. DATA.A = 1;
  8. 0x004013f3 <+57;: mov-0x10 (%EBP),%eax
  9. 0x004013f6 <+60;: Movl $0x1, (%eax)
  10. DATA.B = 2;
  11. 0X004013FC <+66;: mov-0x10 (%EBP),%eax
  12. 0X004013FF <+69;: Movl $0x2,0x4 (%eax)
  13. Const Tdata &data1 = test.getdata ();
  14. 0x00401406 <+76;: lea-0x24 (%EBP),%eax
  15. 0x00401409 <+79;: mov%eax, (%ESP)
  16. 0x0040140c <+82: Call 0x406440 <_zn5ctest7getdataev>
  17. 0x00401411 <+87;: mov%eax,-0xc (%EBP)

From the disassembly above we see that the functions of the two calls are the same, all 0x406440 <_zn5ctest7getdataev> This is left to the reader to solve? Welcome everyone to actively answer?

http://blog.csdn.net/rabinsong/article/details/9708529

Return only functions of different types, how to implement overloading in C + +?

Related Article

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.