在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. 一个带动画效果的颜色选择对话框控件AnimatedColorPickerDialog

    android4.4的日历中选择日程显示颜色的时候有一个颜色选择对话框非常漂亮,模仿他的界面我实现了一个类似的对话框,而且带有动画效果. 代码的实现可讲的地方不多,主要是采用了和AlertDialog ...

  2. 搭建Windows SVN服务器及TortoiseSVN使用帮助和下载

    搭建Windows SVN服务器: 用的SVN服务器通常为外部,例如Google Code的服务器,不过,做为一个程序开发人员,就算自己一个人写程序,也应该有一个SVN版本控制系统,以便对开发代码进行 ...

  3. 6个最佳的开源Python应用服务器

    6个最佳的开源Python应用服务器 首先,你知道什么是应用服务器吗?应用服务器通常被描述为是存在于服务器中心架构中间层的一个软件框架. AD: 首先,你知道什么是应用服务器吗?应用服务器通常被描述为 ...

  4. 关于PEER - PEER毅恒挚友 - Powered by Discuz!

    关于PEER - PEER毅恒挚友 - Powered by Discuz! PEER Experience Exchange Rostrum(PEER)由海外中国留学生和中国知名高校学生组成,服务中 ...

  5. 测试 __try, __finally, __except(被__finally捕获的异常, 还会被上一级的__except捕获。反之不行)

    C语言标准是没有 try-catch语法 的, M$家自己提供了一组. /// @file ClassroomExamples.c /// @brief 验证C语言的非标准try, catch #in ...

  6. listbox多选实现上下移动 js版和服务器版

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200906021128.as ...

  7. Koa -- 基于 Node.js 平台的下一代 web 开发框架

    http://koa.bootcss.com/ 多研究点 react 和 nodejs 这个是未来

  8. 【图像识别】 图像处理和图像分析(leptonica)leptonica-1.68安装配置 (vs2008)

    Leptonica Leptonica is a pedagogically-oriented open source site containing software that is broadly ...

  9. u盘安装ubuntu10.04 server.txt

    10.04 先将 ubuntu server 的 iso 放到优盘上,然后在提示无法找到光驱时,按 alt+f2 打开一个新的 console 窗口,将 iso mount 上,具体操作如下: ls ...

  10. Codeforces Round #270--B. Design Tutorial: Learn from Life

    Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 256 megabytes in ...