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

                                                                                        using change access level

                

神奇的c++特性:using 改变访问级别_第1张图片


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;
}


你可能感兴趣的:(神奇的c++特性:using 改变访问级别)