so true

心怀未来,开创未来!
随笔 - 160, 文章 - 0, 评论 - 40, 引用 - 0
数据加载中……

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()

posted on 2015-03-03 14:34 so true 阅读(265) 评论(0)  编辑  收藏 所属分类: C&C++


只有注册用户登录后才能发表评论。


网站导航: