C++ 与 java重载区别

来源:互联网 发布:完全消耗系数矩阵 编辑:程序博客网 时间:2024/05/21 12:44
C++ 重载只能是同一个类中,他会优先查找子类的函数,如果有相同的的函数名,即使参数不匹配,他也不会去父类里面去插在,子类的同名函数会隐藏父类的函数,不过可以用using directive 去把父类的方法引进到子类的名字空间。

#include<iostream>
using namespace std;

class Base {
public:
    
void foo (int i) {
        cout
<<"Base  int "<<endl;;
    }

    
void foo () {
        cout
<<"Base  none "<<endl;
    }

}
;

class Drived: public Base {
public:
    
void foo (string s) {
        cout
<<"Drived  string"<<endl;
    }

}
;

int main()
{
    Drived  derive;     
    derive.foo(
"string ");  // error
    return 0;
}

而Java不会屏蔽其在基类的任何版本
还有就是java不能重载运算符;
原创粉丝点击