1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using   namespace  std;
class  A{
     public :
         void  foo( void ){
             cout <<  "A::foo(void)"  << endl;
         }
};
 
class  B: public  A{
     public :
         void  foo( int  i){
             cout <<  "B::foo(int)"  << endl;
         }
         void  bar( void ){
             foo(); //这里不写using A::foo;就会报错,因为B类中有个foo(int i)
             //函数,会覆盖A类中的同名函数,但是B类中的foo是有参的,调用foo()肯定报错
             //使用using A::foo;
         }
         using  A::foo;
};
 
int  main( void ){
     B b;
     b.bar(); //调用A类中的foo函数
     return  0;
}


此时我们修改B类,其他不变

1
2
3
4
5
6
7
8
9
10
11
12
13
class  B: public  A{
     public :
         void  foo( void ){
             cout <<  "B::foo(int)"  << endl;
         }
         void  bar( void ){
             foo(); //A::foo();
         }
         using  A::foo; //使用using A::foo将基类中的类在此类中可见,但是
                       //此时B中的foo和A中的foo一模一样,所以会此时会调用B中的
                       //foo,如果要想调用A中的foo;直接A::foo()就好了.
       
};