在C++primer 第4版的 15章 15.2.5中有以下这样一段提示:

“注解:派生类能够恢复继承成员的訪问级别,但不能使訪问级别比基类中原来指定的更严格或者更宽松。”

在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声明对基类权限的影响)的更多相关文章

  1. Spring中的一个错误:使用Resources时报错(The annotation @Resources is disallowed for this location)

    在学习Spring的过程中遇到一个错误:在使用注解@resources的时候提示:The annotation @Resources is disallowed for this location 后 ...

  2. Jenkins. 安装过程中出现一个错误: No such plugin: cloudbees-folder

    安装过程中出现一个错误: No such plugin: cloudbees-folder 安装插件,有时候会报类似的错误:An error occurred during installation: ...

  3. RDP 协议组件 X.224 在协议流中发现一个错误并且中断了客户端连接

    如果你的服务器有如下错误: “RDP 协议组件 X.224 在协议流中发现一个错误并且中断了客户端连接.” 可能的有2种: 1:你试试能否能继续远程登陆,有可能你的远程登陆组件出现问题. 2:有人攻击 ...

  4. SpringMVC form:form的一个错误(没有传到前台绑定类)

    SpringMVC form:form的一个错误(没有传到前台绑定类) 报错信息: Neither BindingResult nor plain target object for bean nam ...

  5. K&R《C语言》书中的一个Bug

    最近在重温K&R的C语言圣经,第二章中的练习题2-2引起了我的注意. 原题是: Write a loop equivalent to the for loop above without us ...

  6. PageRank简单实现中的一个错误

    在我的一篇博客PageRank中,在5.1 算法实现中简单实现部分原本是有一个错误的.这个错误也体现出我当时对PageRank算法有理解上的偏差. 这是个什么样的错误呢?是这样的: 简单实现中计算每个 ...

  7. Visual Studio 2015在.NET Core RC2项目中的一个错误。

    更新了.NET Core RC2 之后,VS的Web Tools更新为“Preview 1”了. 这个版本有一个问题,害我折腾了一个下午. 就是在项目界面的“依赖项 - NPM”上面错误地显示了不必要 ...

  8. 安装过程中出现一个错误: No such plugin: cloudbees-folder

    上面的错误显示是,安装插件cloudbees-folder失败,是因为下载的Jenkins.war里没有cloudbees-folder插件 需要去 https://updates.jenkins-c ...

  9. Java 从原字符串中截取一个新的字符串 subString()

    Java 手册 substring public String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串.该子字符串从指定索引处的字符开始,直 ...

随机推荐

  1. PHP - 字符串操作

    第8章 字符串处理 学习要点: 1.字符串格式化 2.操作子字符串 3.字符串比较 4.查找替换字符串 5.处理中文字符 在每天的编程工作中,处理.调整以至最后控制字符串是很重要的一部分,一般也认为这 ...

  2. Random Teams

    n participants of the competition were split into m teams in some manner so that each team has at le ...

  3. 基于visual Studio2013解决面试题之1002公共子串

     题目

  4. IntelliJ Idea 经常使用快捷键列表

    Alt+回车 导入包,自己主动修正Ctrl+N   查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码 Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如 ...

  5. 王立平--eclipse向svnserver上传项目

    1.team-->share project watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2 ...

  6. S3C3440看门狗驱动程序

    S3C3440看门狗驱动程序 http://www.cnblogs.com/lfsblack/archive/2012/09/13/2684079.html 看门狗是当CPU进入错误状态后,无法恢复的 ...

  7. 体验魅力Cognos BI 10 系列,第1 部分: 第一次安装

    体验魅力Cognos BI 10 系列,第1 部分: 第一次安装吴敏达, 信息管理软件高级技术顾问, IBM简介: 本系列教程旨在帮助您通过实际动手掌握Cognos BI 10.1 的主要功能.在这一 ...

  8. c语言,gdb

    Get gdb call stack http://blog.csdn.net/zoufeiyy/article/details/1490241 Debugging with GDB - Examin ...

  9. 性能测试之LoadRunner11 破解

    1. 下载破解文件lm70.dll和mlr5lprg.dll     lm70.dll文件,覆盖x:\Program Files\Mercury\LoadRunner\bin下文件即可.     ml ...

  10. Android用surface直接显示yuv数据(二)

    上一篇文章主要是參照AwesomePlayer直接用SoftwareRenderer类来显示yuv,为了能用到这个类,不惜依赖了libstagefright.libstagefright_color_ ...