码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++ 函数:在相同类型参数情况下返回不同类型值


    首先重载是不能做到的,相同函数参数类型前提下,不可能返回不同类型的返回值,但是想弄一些歪招实现这个效果

    模板不算,因为模板会根据不同的输入参数类型生成不同的函数,就不是调用相同的函数了

    1、使用 void* 的返回值

    然后自己强制转类型,类似 malloc

    void* test(int i);

    2、类似使用工厂模式

    使用虚函数

    1. class base {
    2. public:
    3. virtual void print()
    4. {
    5. cout << "base\n";
    6. }
    7. };
    8. class father : public base {
    9. public:
    10. void print()
    11. {
    12. cout << "father\n";
    13. }
    14. };
    15. base* test(int i) {
    16. if (i == 1)
    17. return new father();
    18. else
    19. return new base();
    20. }

    3、使用 operator TypeName()

    operator TypeName() 可以把一种类当另外一个类型用

    1. #include
    2. using namespace std;
    3. class Foo {
    4. public:
    5. operator bool() const {
    6. return true;
    7. }
    8. operator int() const {
    9. return 2;
    10. }
    11. operator string() const {
    12. return "string";
    13. }
    14. };
    15. int main(int argc, const char* argv[]) {
    16. Foo foo1;
    17. bool b = foo1;
    18. int i = foo1;
    19. string s = foo1;
    20. cout << b << endl;
    21. cout << i << endl;
    22. cout << s << endl;
    23. return 0;
    24. }
    25. /*
    26. * 输出
    27. 1
    28. 2
    29. string
    30. */

    基于此原理,我们可以制作一个 test 函数,实现文章开头提到的功能

    1. #include
    2. using namespace std;
    3. class Foo {
    4. public:
    5. Foo(bool b1, int i1, string s1) {
    6. b = b1;
    7. i = i1;
    8. s = s1;
    9. }
    10. operator bool() const {
    11. return b;
    12. }
    13. operator int() const {
    14. return i;
    15. }
    16. operator string() const {
    17. return s;
    18. }
    19. bool b;
    20. int i;
    21. string s;
    22. };
    23. Foo test(bool b, int i, string s) {
    24. return Foo(b, i, s);
    25. }
    26. int main(int argc, const char* argv[]) {
    27. bool b = test(1, 2, "3");
    28. int i = test(1, 2, "3");
    29. string s = test(1, 2, "3");
    30. cout << b << endl;
    31. cout << i << endl;
    32. cout << s << endl;
    33. return 0;
    34. }
    35. /*
    36. * 输出
    37. 1
    38. 2
    39. 3
    40. */

  • 相关阅读:
    jquery html(““)造成内存上涨
    合肥工业大学计算机网络课设-在线留言板
    EIP-3664合约研究笔记06--text功能分析
    【SA8295P 源码分析】89 - QNX AIS Camera qcarcam_test 可执行程序 main() 函数 源代码流程分析
    2022-6-29 最大二叉树,根据前序和后序遍历构造二叉树,将子数组重新排序得到同一个二叉查找树的方案数
    Docker error
    离线方式安装supervisor
    面试题:海量数据处理利器-布隆过滤器
    MapStruct与lombok加载顺序问题与annotationProcessorPaths的关系?
    Js逆向教程-13浏览器和JS的关系/伪造浏览器环境 )
  • 原文地址:https://blog.csdn.net/qq_38781075/article/details/134085839
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号