name hiding in C++

name hiding in C++

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdint.h>
#include <pthread.h>
#include <vector>
#include <map>
#include <set>
using namespace std;
void fun() {
    cout << __PRETTY_FUNCTION__ << endl;
}
namespace {
    void fun(int i) {
        cout << __PRETTY_FUNCTION__ << endl;
        ::fun(); //use fun() will compile error
    }
}
class A {
public:
    void fun() {
        cout << __PRETTY_FUNCTION__ << endl;
    }
    void fun(int x) {
        cout << __PRETTY_FUNCTION__ << endl;
    }
};
class B: public A {
public:
    //using A::fun; //solution 3
    void fun(int x) {
        cout << __PRETTY_FUNCTION__ << endl;
    }
};
int main(int argc, char* argv[]) {
    B b;
    //b.fun();//compile error, name hiding occurs
    b.A::fun(); //solution 1
    A* pa = &b;
    pa->fun();  //solution 2
    fun(3);
    return 0;
}

===================   run result   ===================
$ make name_hiding && ./name_hiding
void A::fun()
void A::fun()
void<unnamed>::fun(int)
void fun()

你可能感兴趣的:(name hiding in C++)