C ++ overloaded functions in support of, and know what function overloading has several features:
1. C ++ function overloading Review
- Overloaded functions essentially independent of different functions
- By C ++ function name and function parameters determine the function call
- Not directly through the function name to get overloaded function entry address
- Function overloading necessarily occur in the same scope
Function overloading 2. C ++ classes
Class member functions can be overloaded, including
- Overloaded constructor
- Overloaded ordinary member function
- Overloaded static member function
Note: Function Overloading must occur in the same scope, and therefore the class member functions and global functions can not constitute a heavy load.
#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()
{
printf("void func()\n");
}
void func(int i)
{
printf("void func(int i), i = %d\n", i);
}
int main()
{
func(); // void func()
func(1); // void func(int i), i = 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;
}
Overloaded meaning
- Prompt function by function name of the function
- Prompt use of the function parameter list
- Expansion of the system already exists in the function function
#include <stdio.h>
#include <string.h>
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, sizeof(buf) - 1);
printf("%s\n", buf);
return 0;
}