文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>c++ 重载

c++ 重载

时间:2010-12-07  来源:崖山.

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

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

view plaincopy to clipboardprint?
  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. }  
namespace base{ int max(int a, int b);}namespace derived{ double max(double a, double b);}void test1(){ using base::max; max(12.12, 3.26); // int max(int, int);// 在这个作用域里只有一个max// 就是int max(int, int);// 即使调用的实际参数是 double, double, 也只能选中这个(通过double->int的转换)// 不会选到derived::max}void test2(){ using derived::max; max(1212, 326); // double max(double, double);// 同上, 不会选到base::max}

那么:

view plaincopy to clipboardprint?
  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();  
struct B{ void f();};struct D : B{ void f(int );};D d; // D确实通过继承得到B::f();d.f(); // 只是这个作用域里的f只有 D::f(int), 没有 B::f()。d.B::f(); // 显示调用通过继承得到的B::f();

4.override  覆盖

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

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

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

view plaincopy to clipboardprint?
  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. }  
struct B{ virtual ~B(); virtual void f(int ); virtual void f(double);};void test(B* b){// 这两个调用是由overload决议// 代码一旦编译完成, 就固定死了, 没得更改。 b->f(1212); // B::f(int); b->f(3.26); // B::f(double); delete b;}

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

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

view plaincopy to clipboardprint?
  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, 根据实际参数来决议  
struct D1 : B{ void f(double); using B::f;}D1 d;d.f( ... ); // 在这里, 重载候选就有 D1::f与B::f, 根据实际参数来决议

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

view plaincopy to clipboardprint?
  1. struct D1 : B {};  
  2. test(new D1);  
  3. // D1没有override任何虚函数, 所以上面两个调用最终是B::f(int)和B::f(double)  
struct D1 : B {};test(new D1);// D1没有override任何虚函数, 所以上面两个调用最终是B::f(int)和B::f(double) view plaincopy to clipboardprint?
  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);  
struct D2 : B{ void f(int );};test(new D2);// D2有override一个: B::f(int), 所以, 最终调用是 D2::f(int), B::f(double); view plaincopy to clipboardprint?
  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. }  
#include <stdio.h>#include <string.h>int main(int argc, char* argv[]){ char const* name = "D1"; if (argc>1) name = argv[1]; B* b = 0; if (strcmp(name, "D1")==0) b = new D1; else if (strcmp(name, "D2")==0) b = new D2; ... else { fprintf(sterr,"no derived class named %s", name); return -1; } test(b); return 0;}

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

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

相关阅读 更多 +
排行榜 更多 +
野生恐龙射击生存安卓版

野生恐龙射击生存安卓版

飞行射击 下载
战场狙击手

战场狙击手

飞行射击 下载
无尽的三月七h5

无尽的三月七h5

休闲益智 下载