你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

C++——类成员函数重载

[复制链接]
gaosmile 发布时间:2020-8-12 20:55
一、类中的函数重载:
1、函数重载的温习:
  • 函数重载的本质为相互独立的不同函数
  • C++中通过函数名和函数参数确定函数调用
  • 无法直接通过函数名得到函数重载函数的入口地址
  • 函数重载必然发生在同一个作用域中

2、类中的成员函数可以进行重载:
  • 构造函数的重载
  • 普通成员函数的重载
  • 静态成员函数的重载

这里有一个问题:全局函数,普通成员函数以及静态成员函数之间是否可以构成重载?
从上面回顾重载函数的知识中,我们要注意到一点函数重载必须发生在同一作用域里面(其他两点问题不大),所以的构造函数和普通成员函数是可以构造重载的,而与全局函数是不可以构成重载的。
代码测试:
#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();
    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;
}

输出结果:
root@txp-virtual-machine:/home/txp# ./a.out
void func()
void func(int i),i=1
Test::Test()
Test::Test(int i)
Test::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()

3、重载的意义:
  • 通过函数名对函数功能进行提提示
  • 通过参数列表对函数用法进行提示
  • 扩展系统中已经存在的函数功能

这里用c语言里面的拷贝字符串函数strcpy来进行扩展演示:
代码版本一:
#include <stdio.h>
#include <string.h>

int main()
{
    const char* s = "linux is great !";
    char buf[8] = {0};
   
    strcpy(buf, s);
   
   
    printf("%s\n", buf);
   
    return 0;
}

输出结果:
root@txp-virtual-machine:/home/txp# ./a.out
linux is great !
*** stack smashing detected ***: ./a.out terminated
Aborted (core dumped)

这里虽然结果是输出多了,但是这个程序同时也报了段错误,因为buf所能存储的能力小于s;所以为了解决这个问题,你肯定第一时间想到strncpy函数:
代码版本二:
#include <stdio.h>
#include <string.h>

int main()
{
    const char* s = "linux is great !";
    char buf[8] = {0};
   
    strncpy(buf, s,sizeof(buf)-1);
   
   
    printf("%s\n", buf);
   
    return 0;
}

输出结果:
root@txp-virtual-machine:/home/txp# ./a.out
linux i

这个函数就保护程序的安全性;但是我在c++里面既然学习了函数重载,那么久可以在原有的函数基础上进行扩展:
代码版本三:
#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 = "linux is great !";
    char buf[8] = {0};
   
    strcpy(buf, s, sizeof(buf)-1);
   
    printf("%s\n", buf);
   
    return 0;
}

输出结果:
root@txp-virtual-machine:/home/txp# ./a.out
linux i

二、总结:
  • 类的成员函数之间可以进行重载
  • 重载必须发生在同一个作用域中
  • 全局函数和成员函数不能构成重载关系
  • 重载的意义在于扩展已经存在的功能


好了,今天的分享就到这里,如果文章中有错误或者不理解的地方,可以交流互动,一起进步。

收藏 评论0 发布时间:2020-8-12 20:55

举报

0个回答

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版