Function overloading in class--Ditty Software College

Keywords: C

The content of the article comes from Tang's courseware of Ditai Software College.
1. Review of Function Overload

  1. The essence of function overload is different functions which are independent of each other.
  2. Function Call Determined by Function Name and Function Parameters in C++
  3. You cannot get the entry address of the overloaded function directly through the function name.
  4. 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?

  1. The essence of overloaded functions is multiple different functions.
  2. Function names and parameter lists are unique identifiers
  3. 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:

  1. Hint function by function name
  2. Hint function usage through parameter list
  3. 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:

  1. Class member functions can be overloaded
  2. Overload must occur in the same scope
  3. Global functions and membership functions cannot constitute overload relations
  4. The significance of overloading is to extend existing functions.

Posted by corkg on Tue, 01 Oct 2019 04:38:42 -0700