C++继承中的名字查找

#include<iostream>
#include<string>
using namespace std;
class Base {
 public:
  void func() {
    cout << "func() in Base." << endl;
  }
  void func(int a) {
    cout << "func(int a) in Base." << endl;
  }
  void func(string s) {
    cout << "func(string s) in Base." << endl;
  }
};


class Derived : public Base { 
public:
  //using Base::func;
  void print() {
    cout << "func() in Derived." << endl;
  }
};


int main() {
  Derived d;
  d.Base::func();//指定基类版本
  d.func();
  d.func(12);//error,可在派生类中添加using Base::print;
  d.func("abc");//error,可在派生类中添加using Base::print;
  system("pause");
  return 0;
}
//一个对象,引用,指针的静态类型决定了该对象哪些成员可见。
//派生类作用域嵌套在基类作用域中
//派生类成员将屏蔽同名的基类成员
//如果派生类想通过自身类型使用的基类中重载版本,则派生类必须要么覆盖所有重载版本,要么一个也不覆盖。
//利用using声明可把基类中的所有重载版本都添加到派生类作用域中
//名字查找先于类型检查,如果在派生类中找到了调用的函数名,则不会继续向上查找,接着进行类型检查

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值