c++ 重载

http://bbs.chinaunix.net/space.php?uid=7672252   thanks OwnWaterloo

heap 堆

stack 栈

overload  重载

override 覆盖

hide        隐藏

replace    替换

有时候容易混淆上面的4中情况为重载

区别:

1.replace

c++ 提供了默认的  void* operator new(size_t s);

而我写了一个自己的版本就是 替换replace;

2.overload

int min(int a, int b);

double min(double a, double b);

具有相同名字, 但是参数不同的函数之间互为overloaded

根据调用处的实际参数,决定了实际的调用的函数

3.hide

上述 相同的名字 有一个前提:在一个查找的作用域内


  1. namespace base  
  2. {  
  3.       int max(int a, int b);  
  4. }  
  5.   
  6. namespace derived  
  7. {  
  8.       double max(double a, double b);  
  9. }  
  10.   
  11. void test1()  
  12. {  
  13.       using base::max;  
  14.       max(12.12, 3.26); // int max(int, int);  
  15. // 在这个作用域里只有一个max  
  16. // 就是int max(int, int);  
  17. // 即使调用的实际参数是 double, double, 也只能选中这个(通过double->int的转换)  
  18. // 不会选到derived::max  
  19. }  
  20.   
  21. void test2()  
  22. {  
  23.       using derived::max;  
  24.       max(1212, 326); // double max(double, double);  
  25. // 同上, 不会选到base::max  
  26. }  

那么:


  1. struct B  
  2. {  
  3.       void f();  
  4. };  
  5.   
  6. struct D : B  
  7. {  
  8.       void f(int );  
  9. };  
  10.   
  11. D d;      // D确实通过继承得到B::f();  
  12. d.f();    // 只是这个作用域里的f只有 D::f(int), 没有 B::f()。  
  13. d.B::f(); // 显示调用通过继承得到的B::f();  

4.override  覆盖

通过基类的指针或者引用操作某个派生类的对象,调用某个虚函数

派生类可以通过override,使得该虚函数调用派生类中的版本,而不是基类的。

overload事编译时的概念,override 试运行时的概念


  1. struct B  
  2. {  
  3.       virtual ~B();  
  4.       virtual void f(int );  
  5.       virtual void f(double);  
  6. };  
  7.   
  8. void test(B* b)  
  9. {  
  10. // 这两个调用是由overload决议  
  11. // 代码一旦编译完成, 就固定死了, 没得更改。  
  12.       b->f(1212);  // B::f(int);  
  13.       b->f(3.26);  // B::f(double);  
  14.       delete b;  
  15. }  

这一类叫做 hide 派生类中的名字会隐藏基类中的名字,使得基类的名字没有参与重载的资格

但是与上面的名字空间的例子类似,可以通过using 引入基类的名字


  1. struct D1 : B  
  2. {  
  3.       void f(double);  
  4.       using B::f;  
  5. }  
  6.   
  7. D1 d;  
  8. d.f( ... ); // 在这里, 重载候选就有 D1::f与B::f, 根据实际参数来决议  

运行时可以通过传入不同的B的派生类 影响test的最终行为


  1. struct D1 : B {};  
  2. test(new D1);  
  3. // D1没有override任何虚函数, 所以上面两个调用最终是B::f(int)和B::f(double)  

  1. struct D2 : B  
  2. {  
  3.       void f(int );  
  4. };  
  5. test(new D2);  
  6. // D2有override一个: B::f(int), 所以, 最终调用是 D2::f(int), B::f(double);  


  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main(int argc, char* argv[])  
  5. {  
  6.       char const* name = "D1";  
  7.       if (argc>1) name = argv[1];  
  8.       B* b = 0;  
  9.       if (strcmp(name, "D1")==0) b = new D1;  
  10.       else if (strcmp(name, "D2")==0) b = new D2;  
  11.       ...  
  12.       else  
  13.       {  
  14.             fprintf(sterr,"no derived class named %s", name);  
  15.             return -1;  
  16.       }  
  17.       test(b);  
  18.       return 0;  
  19. }  

gcc xxx.cpp -o a.out  编译完毕后, a.out的行为依然可以修改:
a.out D1
a.out D2

overload的影响在编译时决定, 不会拖到运行时。
override的影响在编译时会决定一部分, 最终还会被运行时影响。

原文链接: https://www.cnblogs.com/orange_zr/archive/2010/12/07/1898947.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++ 重载

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/18430

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午7:14
下一篇 2023年2月7日 下午7:14

相关推荐