您的位置:首页 > 编程语言 > C语言/C++

神奇的c++特性:using 改变访问级别

2012-08-18 15:31 495 查看
using change access level



Originally, the member function size() in class derived 's access level is private, but the using change it to public

using also can solve the problem that derived class can't overload base class's member(reference C++ PRIMER 15.5.3)

#include <iostream>
struct Base
{
int memfcn()
{
return 1;
}
};
struct Derived:Base
{
public:
using Base::memfcn;//withnot "using ...",we can't access Derived::memfcn()
int memfcn(int)//it will make Derived class hide the base's member memfcn
// within the scope of the Derived class
{
return 2;
}

};
struct child:Derived
{

};
int main()
{
Derived d;
Base b;
b.memfcn();
d.memfcn(10);
std::cout<<d.memfcn()<<std::endl;
std::cout<<d.Base::memfcn()<<std::endl;
std::cout<<d.memfcn(2)<<std::endl;
child ch;
ch.memfcn();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: