在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. C++ 左值 右值

    最近在研究C++ 左值 右值,搬运.收集了一些别人的资料,供自己记录和学习,若以后看到了更好的解释,会继续补充.(打“?”是我自己不明白的地方 )   参考:<Boost程序库探秘——深度解析C ...

  2. BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )

    Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...

  3. hdu 1760 一道搜索博弈题 挺新颖的题目

    A New Tetris Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. git使用说明

    1,git clone git://github.com/schacon/simplegit.git git工作目录,暂存目录,本地代码仓库都有代码了. 2,git pull git://github ...

  5. android中保存Bitmap图片到指定文件夹中的方法

    /** 保存方法 */  public void saveBitmap() {   Log.e(TAG, "保存图片");   File f = new File("/s ...

  6. 研究一下TForm.WMPaint过程(也得研究WM_ERASEBKGND)——TForm虽然继承自TWinControl,但是自行模仿了TCustomControl的全部行为,一共三种自绘的覆盖方法,比TCustomControl还多一种

    先擦除背景: procedure TCustomForm.WMEraseBkgnd(var Message: TWMEraseBkgnd); begin if not IsIconic(Handle) ...

  7. 利用iframe技巧获取訪问者qq

    今天工作时,有个暂时加的好友问我,怎么利用web页面获取訪问者的qq. 曾经没有接触过,感觉到非常好奇,可是工作中脑子非常亢奋,转的快,利用所学的知识迅速想到一条技巧,那就是假想用户在进入我们设定的页 ...

  8. hadoop出现ava.lang.ClassNotFoundException: org.codehaus.jackson.map.JsonMappingException

    Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jackson/map/JsonMa ...

  9. 枚举算法总结 coming~^.*

    感谢CJ同学监督╭(╯^╰)╮.从放假到现在都木有更新博客了~噶呜~小娘谨记教诲,每天会更新博客==!! 看了一下POJ训练计划,虽然已经零零散散做了40多道题了,还是从头开始整理一下漏掉的知识点.T ...

  10. [Android学习笔记]Context简单理解

    一.Context是什么?上下文对象,可以理解为一个程序的运行的环境,从中可以获取当前程序的资源:getResources,getAssets 二.常见的Context有哪些?Application ...