29.类中的函数重载
/* 1.函数重载的本质为相互独立的不同函数 2.C++中通过函数名和函数参数确定函数调用 3.无法直接通过函数名得到重载函数的入口地址 4.函数重载必然发生在同一个作用域中 */ /* 类中的成员函数可以进行重载: 1.构造函数 2.一般的成员函数 3.静态成员函数 */ #include <iostream> using namespace std; class Test { private: int i; public: Test() { cout << "Test::Test()" << endl; this->i = 0; } Test(int i) { cout << "Test::Test(int i)" << endl; this->i = i; } Test(const Test &obj) { cout << "Test(const Test &obj)" << endl; this->i = obj.i; } static void func() { cout << "void Test::func()" << endl; } void func(int i) // 作用域不同,与全局func函数不构成重载 { cout << "void Test::func(int i), i = " << i << endl; } int getI() { return i; } }; void func() { cout << "void func()" << endl; } void func(int i) { cout << "void func(int i), i = " << i << endl; } int main() { func(); func(1); cout << endl; Test t; Test t1(1); Test t2(t1); cout<<endl; func(); Test::func(); // 静态成员函数可以通过类名直接调用 cout << endl; func(2); t1.func(2); t1.func(); return 0; }

更多精彩