The content of the article comes from Tang's courseware of Ditai Software College.
1. Review of Function Overload
- The essence of function overload is different functions which are independent of each other.
- Function Call Determined by Function Name and Function Parameters in C++
- You cannot get the entry address of the overloaded function directly through the function name.
- Function overload must occur in the same scope
2. Membership functions in classes can be overloaded
- Constructor
- Overload of General Membership Functions
- Overload of static member functions
Question: Can global functions, ordinary member functions and static member functions be overloaded?
- The essence of overloaded functions is multiple different functions.
- Function names and parameter lists are unique identifiers
- Function overloading must occur in the same scope (global functions cannot be overloaded with functions in other domains)
Case analysis: comprehensive analysis of class and overload
#include <stdio.h> class Test { int i; public: Test() { printf("Test::Test()\n"); this->i = 0; } Test(int i) { printf("Test::Test(int i)\n"); this->i = i; } Test(const Test& obj) { printf("Test(const Test& obj)\n"); this->i = obj.i; } static void func() { printf("void Test::func()\n"); } void func(int i) { printf("void Test::func(int i), i = %d\n", i); } int getI() { return i; } }; void func()//And the void func() function in the class does not constitute overload { printf("void func()\n"); } void func(int i)//And the void func() function in the class does not constitute overload { printf("void func(int i), i = %d\n", i); } int main() { func(); func(1); Test t; // Test::Test() Test t1(1); // Test::Test(int i) Test t2(t1); // Test(const Test& obj) func(); // void func() Test::func(); // void Test::func() func(2); // void func(int i), i = 2; t1.func(2); // void Test::func(int i), i = 2 t1.func(); // void Test::func() return 0; } void func() void func(int i), i = 1 Test::Test() Test::Test(int i) Test(const Test& obj) void func() void Test::func() void func(int i), i = 2 void Test::func(int i), i = 2 void Test::func()
In-depth significance:
The significance of overloading:
- Hint function by function name
- Hint function usage through parameter list
- Extending the functions already existing in the system
Example: Significance analysis of overloading
#include <stdio.h> #include <string.h> //The strcpy function in C language is extended by function overloading. Extended functionality char* strcpy(char* buf, const char* str, unsigned int n) { return strncpy(buf, str, n); } int main() { const char* s = "D.T.Software"; char buf[8] = {0}; //strcpy(buf, s); strcpy(buf, s, sizeof(buf)-1); printf("%s\n", buf); return 0; }
Think: Overloading can extend the functions already existing in the system, so can overloading expand more functions?
Is the following plural solution feasible?
Conclusion:
- Class member functions can be overloaded
- Overload must occur in the same scope
- Global functions and membership functions cannot constitute overload relations
- The significance of overloading is to extend existing functions.