class Base {
int num = 10;
protected:
int show() {
return num;
}
};
class Derive : Base {
public:
using Base::show;//这个写法好怪,原理是啥?编译器会做什么操作呢?
};
int main()
{
Derive d = Derive();
int num = d.show();//为什么using以后,这边就可以调用show了呢,背后的原理是什么
return 1;
}
using只不过是在派生类中改变了继承自基类成员的可访问权限而已,编译器就在语法分析阶段判断你的访问是否符合定义的权限。其它啥也没做。
C++ 11的特性
https://blog.csdn.net/jiemashizhen/article/details/125016154
https://blog.csdn.net/wishchin/article/details/79870177
这是C++11引入的新语法,就是把基类的成员的名字引入到派生类中。
https://en.cppreference.com/w/cpp/language/using_declaration#In_class_definition
Using-declaration introduces a member of a base class into the derived class definition, such as to expose a protected member of base as public member of derived. In this case, nested-name-specifier must name a base class of the one being defined. If the name is the name of an overloaded member function of the base class, all base class member functions with that name are introduced. If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.