C++子类函数为什么不能重载父类函数

先说结论:
子类成员函数的函数名和基类一样,但是函数声明与基类不一样的时候,不会和基类函数构成重载,而是会隐藏基类函数

简要回顾下C++中的基本概念:

  • 重写(override): 基类函数带virtual,子类函数声明和基类完全一样,实现不一样
  • 重载(overload): 同一个类中,函数名一样,函数参数类型,个数,顺序等不一样的构成重载
  • 隐藏(hide): 子类函数名和基类一样,但是函数声明与基类不一样,就会对基类的函数进行隐藏

那么,子类函数名和基类一样,但是函数声明与基类不一样的时候,为什么不能重载基类函数呢?
先看例子一

#include <stdio.h>

class Base {
public:
    virtual void foo(float a) {
        printf(" Base :: foo(float) \n");
    };

    virtual void foo(double a) {
        printf(" Base :: foo(double) \n");
    };
};

class Derived : public Base {
public:
    virtual void foo(double a) {
        printf(" Derived :: foo(double) \n");
    };

};

int main() {
    Derived d;
    float a = 3.0f;
    d.foo(a);

    Base b;
    b.foo(a);
}

如果重载可以发生在子类和基类之间,函数调用d.foo(a)的最佳匹配应该是Base::foo(float a),而实际输出是

Derived :: foo(double)

说明float类型的a向上转型为double,调用了子类的函数,重载没有在子类和基类间发生。这里如果类型转换不能发生,将不能通过编译。

而b.foo(a)的输出为:

Base :: foo(float) 

这说明重载在单个类内部进行。

如果实在想在子类中调用父类的函数,对于下面的例子二(不能编译通过):

class A
{
public:
  void a() {}
};

class B : public A
{
public:
  void a(int) {}
};

int main()
{
  B b;
  b.a();
}

如果需要上面的函数可以编译通过,我们可以这样做:

  1. 在class B 内部加上using A::a
  2. 调用时使用b.A::a(),不推荐这样做

当然,问题的关键是为什么C++的设计者这么设计,从技术实现来说,访问基类函数来进行名字查找,实现跨类重载没有太大的难度。但是从实际用户意图来说,像上面B中添加void a(int)函数的目的就是为了重新实现A的接口,并隐藏原来的接口;当然,如果用户不想隐藏,可以加上using A::a。
另外,对于例子一,如果实现了跨类重载,那么d.foo(a)将也会调用到基类的函数,尽管float可以转型到float, 将很容易引起混淆

顺应用户意图和避免不必要的混淆,这可能就是C++设计者这么设计的原因。

参考网页:

  1. https://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the
  2. https://bastian.rieck.me/blog/posts/2016/name_hiding_cxx/
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个可能的实现: ```c++ #include <iostream> using namespace std; class Animal { protected: string name; int age; public: void set(string n, int a) { name = n; age = a; } virtual void print() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; class Cat : public Animal { string color; public: void set(string n, int a, string c) { Animal::set(n, a); color = c; } void print() { Animal::print(); cout << "Color: " << color << endl; } }; class Dog : public Animal { string breed; public: void set(string n, int a, string b) { Animal::set(n, a); breed = b; } void print() { Animal::print(); cout << "Breed: " << breed << endl; } }; istream& operator>>(istream& is, Animal& a) { string n; int ag; is >> n >> ag; a.set(n, ag); return is; } istream& operator>>(istream& is, Cat& c) { Animal& a = c; is >> a; string col; is >> col; c.set(a.name, a.age, col); return is; } istream& operator>>(istream& is, Dog& d) { Animal& a = d; is >> a; string br; is >> br; d.set(a.name, a.age, br); return is; } int main() { Cat c; Dog d; cout << "Enter cat's name, age, and color: "; cin >> c; cout << "Enter dog's name, age, and breed: "; cin >> d; cout << endl << "Cat:" << endl; c.print(); cout << endl << "Dog:" << endl; d.print(); return 0; } ``` 在这个例子中,Animal类是一个基类,Cat和Dog类是它的两个子类。在Animal类中定义了一个set函数,用于设置姓名和年龄。Cat和Dog类都继承了这个函数,并增加了自己的数据成员。它们也都定义了一个print函数,用于输出所有数据成员。 在主函数中,我们定义了一个Cat对象和一个Dog对象,并且通过输入运算符重载来从标准输入读取数据。输入运算符重载函数使用相应的set函数来设置数据成员。注意,在Cat和Dog的set函数中,我们调用了Animal的set函数来设置姓名和年龄。 最后,我们输出所有数据成员,使用的是各自的print函数。注意,在Cat和Dog的print函数中,我们调用了Animal的print函数来输出姓名和年龄。这里使用了虚函数的特性,使得程序能够正确地调用各自的print函数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值