C++primer原书中的一个错误(派生类using声明对基类权限的影响)
2014-07-25 02:03
267 查看
在C++primer 第4版的 15章 15.2.5中有下面这样一段提示:
“注解:派生类可以恢复继承成员的访问级别,但不能使访问级别比基类中原来指定的更严格或者更宽松。”
在vs2010中经过验证,这段话是错误的。具体见以下代码:
这段程序是可以正常运行没有任何错误的,但是基类Base的成员n权限是protected,在派生类中用using将其提权到了public,这就证明了原书中的那段话是错误的。
但是当我把Base类的protected 成员 n权限改成private的时候却出现了错误,因此猜测:只有子类对成员具有访问权限的时候才能改变成员的访问级别。
后来在http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2540.htm看到这样一段话:
The access rules for inheriting constructors are specified in 12.9 class.inh-ctor; otherwise all All instances of the name mentioned in a using-declaration shall be accessible. In particular, if a derived class uses a using-declaration to access a member of
a base class, the member name shall be accessible. If the name is that of an overloaded member function, then all functions named shall be accessible. The base class members mentioned by a using-declaration shall be visible in the scope of at least one of
the direct base classes of the class where the using-declaration is specified. ...
结果证明我的猜测是正确的。
“注解:派生类可以恢复继承成员的访问级别,但不能使访问级别比基类中原来指定的更严格或者更宽松。”
在vs2010中经过验证,这段话是错误的。具体见以下代码:
//Base.h #pragma once #include <iostream> using namespace std; class Base { public: Base(void); ~Base(void); size_t size()const{return n;} protected: //private: size_t n; };
//Base.cpp #include "Base.h" Base::Base(void) { n = 100; } Base::~Base(void) { }
//Derived.h #pragma once #include "base.h" class Derived : private Base { public: Derived(void); ~Derived(void); using Base::size; using Base::n; };
//Derived.cpp #include "Derived.h" Derived::Derived(void) { } Derived::~Derived(void) { }
//main.cpp #include "Base.h" #include "Derived.h" #include <iostream> using namespace std; void main() { Derived XX; Base YY; cout<<XX.size()<<endl; cout<<XX.n<<endl; }
这段程序是可以正常运行没有任何错误的,但是基类Base的成员n权限是protected,在派生类中用using将其提权到了public,这就证明了原书中的那段话是错误的。
但是当我把Base类的protected 成员 n权限改成private的时候却出现了错误,因此猜测:只有子类对成员具有访问权限的时候才能改变成员的访问级别。
后来在http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2540.htm看到这样一段话:
The access rules for inheriting constructors are specified in 12.9 class.inh-ctor; otherwise all All instances of the name mentioned in a using-declaration shall be accessible. In particular, if a derived class uses a using-declaration to access a member of
a base class, the member name shall be accessible. If the name is that of an overloaded member function, then all functions named shall be accessible. The base class members mentioned by a using-declaration shall be visible in the scope of at least one of
the direct base classes of the class where the using-declaration is specified. ...
结果证明我的猜测是正确的。
相关文章推荐
- C++primer原书中的一个错误(派生类using声明对基类权限的影响)
- [转]c++类继承中的using声明,派生类中用using声明改变基类成员的访问权限
- c++类继承中的using声明,派生类中用using声明改变基类成员的访问权限[zz]
- c++类继承中的using声明,派生类中用using声明改变基类成员的访问权限
- 【protected权限】java浅复制、深复制中,为什么在派生类中覆盖基类的clone()方法,并声明为public
- C++中派生类之间的赋值对基类的影响
- 基类中声明为virtual ,派生类中的同名函数
- overwrite 对基类同名重载函数在派生类中可见性的影响
- 关于基类派生类的继承访问权限
- C++:调整基类成员在派生类中的访问属性的其他方法(同名成员和访问声明)
- MOOC清华《面向对象程序设计》第4章:基类与派生类的继承权限实验
- 区分“派生类对象”和“派生类”对基类成员的访问权限
- 事件只能从声明它们的类中调用,派生类不能直接调用在基类声明的事件
- part4.(two)派生类成员函数和其它函数访问基类成员时的权限(访问控制表)
- c++primer学习笔记-----3.1命名空间的using 声明
- 若使用基类指针delete派生类对象,需将基类和派生类的析构函数声明为virtual
- C++primer学习:面象对象程序设计(1):定义基类和派生类
- 构造函数访问权限和基类构造函数,派生类的构造函数调用顺序
- 派生类对基类成员的访问权限和派生类对象对基类成员的访问权限
- C++ 中基类及派生类中继承成员访问权限