zoukankan      html  css  js  c++  java
  • C++类中的重载

    • 函数重载回顾
    1. 函数重载的本质为相互独立的不同函数
    2. C++中通过函数名函数参数确定函数调用
    3. 无法直接通过函数名得到重载函数的入口地址
    4. 函数重载必然发生在同一个作用域
    • 类中的成员函数可以进行重载
    1. 构造函数的重载
    2. 普通成员函数的重载
    3. 静态成员函数的重载
    • 问题:全局函数,普通成员函数以及静态成员函数之间是否可以构成重载?
    1. 重载函数的本质为多个不同的函数
    2. 函数名和参数列表是唯一的标识
    3. 函数重载必须发生在同一个作用域中
    在类中,普通成员函数以及静态成员函数之间可以构成重载关系,但是全局函数与类中的成员函数不能构成重载关系,因为全局函数位于全局的命名空间,而成员函数位于类中,它们之间的作用域已经不一样了
    #include <stdio.h>
    class Test
    {
        int i;
    public:
        Test()
        {
            printf("Test::Test()
    ");
            this->i = 0;
        }
        Test(int i)
        {
            printf("Test::Test(int i)
    ");
            this->i = i;
        }
        Test(const Test& obj)
        {
            printf("Test(const Test& obj)
    ");
            this->i = obj.i;
        }
        static void func()
        {
            printf("void Test::func()
    ");
        }
        void func(int i)
        {
            printf("void Test::func(int i), i = %d
    ", i);
        }
        int getI()
        {
            return i;
        }
    };
     
    void func()
    {
        printf("void func()
    ");
    }
     
    void func(int i)
    {
        printf("void func(int i), i = %d
    ", 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;
    }
    运行结果:

    pi@raspberrypi:~ $ g++ main.cpp
    pi@raspberrypi:~ $ ./a.out
    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()

     
     
    • 函数重载有什么意义?
    1. 通过函数名对函数功能进行演示
    2. 通过参数列表对函数用法进行提示
    3. 扩展系统中已经存在的函数功能
    #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*str = "hello";
        char buf[3] = {0};
        strcpy(buf,str,sizeof(str)-1);
        printf("%s
    ",buf);
        return 0;
    }
    运行结果:

    pi@raspberrypi:~ $ g++ main.cpp
    pi@raspberrypi:~ $ ./a.out
    hel
    pi@raspberrypi:~ $

     

     
     
     
     
     
     
     
     
    主要记录的是学习听课的笔记
  • 相关阅读:
    eclipse新建web项目开发JSP
    配置tomcat系统日志--java eclipse
    Eclipse 导入外部项目无法识别为web项目并且无法在部署到tomcat下
    for和foreace的区别
    布局TextView和EditText区别,layout_width和lay_weight区别--Android Studio
    设备旋转,创建水平模式布局--Android studio
    关于点击Invalidate Caches/Restart禁止插件后,重新加载--Android Studio
    Android 全面插件化 RePlugin 流程与源码解析
    git分支开发,分支(feature)同步主干(master)代码,以及最终分支合并到主干的操作流程
    git使用流程
  • 原文地址:https://www.cnblogs.com/chengeputongren/p/12174759.html
Copyright © 2011-2022 走看看