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
- typedef struct Tdata {
- int A;
- int b;
- } Tdata;
- Class CTest {
- Public
- Tdata &getdata ()
- {
- return data;
- }
- Const Tdata &getdata ()
- {
- return data;
- }
- Private
- Tdata data;
- };
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
- CTest test;
- 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
- #define  GET_DATA () \
- public:\
- tdata &getdata () { return data; } \
- const TData & GetData () const { return data; }
-   
- typedef struct tdata {
- int a;
- int b;
- } tdata;
-
- CLASS CTEST {  
- get_data ()
- PRIVATE:  
- tdata data;
- };  
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
- int main (int argc, char *argv[])
- {
- Qapplication app (argc, argv);
- CTest test;
- /* Write */
- Tdata &data = test.getdata ();
- data.a = 1;
- data.b = 2;
- /* Read */
- Const Tdata &data1 = test.getdata ();
- Qdebug () << data1.a << data1.b;
- return App.exec ();
- }
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
- CTest test;
- Tdata &data = test.getdata ();
- 0x004013e5 <+43;: lea-0x24 (%EBP),%eax
- 0x004013e8 <+46;: mov%eax, (%ESP)
- 0x004013eb <+49: Call 0x406440 <_zn5ctest7getdataev>
- 0x004013f0 <+54;: mov%eax,-0x10 (%EBP)
- DATA.A = 1;
- 0x004013f3 <+57;: mov-0x10 (%EBP),%eax
- 0x004013f6 <+60;: Movl $0x1, (%eax)
- DATA.B = 2;
- 0X004013FC <+66;: mov-0x10 (%EBP),%eax
- 0X004013FF <+69;: Movl $0x2,0x4 (%eax)
- Const Tdata &data1 = test.getdata ();
- 0x00401406 <+76;: lea-0x24 (%EBP),%eax
- 0x00401409 <+79;: mov%eax, (%ESP)
- 0x0040140c <+82: Call 0x406440 <_zn5ctest7getdataev>
- 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 + +?