C++primer原书中的一个错误(派生类using声明对基类权限的影响)

在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. ...


结果证明我的猜测是正确的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码: ```c++ #include<iostream> #include<string> #include<fstream> #include<vector> #include<algorithm> using namespace std; // 基类:书籍类 class Book { protected: string bookName; // 书名 int bookID; // 书籍编号 public: Book() {} // 默认构造函数 Book(string name, int id) :bookName(name), bookID(id) {} // 带参数的构造函数 virtual void printInfo() {} // 虚函数,用于输出书籍信息 virtual ~Book() {} // 虚析构函数 }; // 派生类:图书类型类 class BookType :public Book { private: string typeName; // 图书类型名称 public: BookType() {} // 默认构造函数 BookType(string name, int id, string type) :Book(name, id), typeName(type) {} // 带参数的构造函数 virtual void printInfo() { // 重载虚函数,输出图书类型信息 cout << "书籍类型:" << typeName << endl; cout << "书籍名称:" << bookName << endl; cout << "书籍编号:" << bookID << endl; } }; // 派生类:图书类 class BookInfo :public Book { private: string author; // 作者 string publisher; // 出版社 double price; // 价格 public: BookInfo() {} // 默认构造函数 BookInfo(string name, int id, string author, string publisher, double price) :Book(name, id), author(author), publisher(publisher), price(price) {} // 带参数的构造函数 virtual void printInfo() { // 重载虚函数,输出图书信息 cout << "书籍名称:" << bookName << endl; cout << "书籍编号:" << bookID << endl; cout << "作者:" << author << endl; cout << "出版社:" << publisher << endl; cout << "价格:" << price << endl; } }; // 图书管理类 class BookManager { private: vector<Book*> books; // 保存所有图书的容器 public: void addBook(Book* book) { // 添加图书 books.push_back(book); } void deleteBook(int id) { // 删除图书 for (auto it = books.begin(); it != books.end(); it++) { if ((*it)->bookID == id) { delete (*it); books.erase(it); break; } } } void modifyBook(int id, string name) { // 修改图书信息 for (auto it = books.begin(); it != books.end(); it++) { if ((*it)->bookID == id) { (*it)->bookName = name; break; } } } void searchBook(int id) { // 查找图书信息 for (auto it = books.begin(); it != books.end(); it++) { if ((*it)->bookID == id) { (*it)->printInfo(); break; } } } void printAll() { // 输出所有图书信息 for (auto book : books) { book->printInfo(); cout << endl; } } void sortByID() { // 按编号排序 sort(books.begin(), books.end(), [](Book* a, Book* b) {return a->bookID < b->bookID; }); } void sortByName() { // 按书名排序 sort(books.begin(), books.end(), [](Book* a, Book* b) {return a->bookName < b->bookName; }); } void saveToFile(string fileName) { // 将图书信息保存到文件 ofstream fout(fileName); for (auto book : books) { fout << book->bookName << " " << book->bookID << " "; if (dynamic_cast<BookType*>(book)) { // 判断是否为图书类型 fout << dynamic_cast<BookType*>(book)->typeName << endl; } else { // 否则为图书信息 BookInfo* info = dynamic_cast<BookInfo*>(book); fout << info->author << " " << info->publisher << " " << info->price << endl; } } fout.close(); } void readFromFile(string fileName) { // 从文件读取图书信息 ifstream fin(fileName); string name, type; int id; while (fin >> name >> id >> type) { // 如果读到图书类型信息 BookType* book = new BookType(name, id, type); addBook(book); } while (fin >> name >> id) { // 如果读到图书信息 string author, publisher; double price; fin >> author >> publisher >> price; BookInfo* book = new BookInfo(name, id, author, publisher, price); addBook(book); } fin.close(); } }; int main() { BookManager manager; // 添加图书 BookType* type1 = new BookType("C++ Primer", 1, "计算机科学"); manager.addBook(type1); BookType* type2 = new BookType("Java核心技术", 2, "计算机科学"); manager.addBook(type2); BookInfo* info1 = new BookInfo("高等数学", 3, "陈红梅", "清华大学出版社", 59.5); manager.addBook(info1); BookInfo* info2 = new BookInfo("线性代数", 4, "韩志刚", "高等教育出版社", 38.0); manager.addBook(info2); // 输出所有图书信息 manager.printAll(); cout << endl; // 修改图书信息 manager.modifyBook(1, "C++ Primer Plus"); // 删除图书 manager.deleteBook(4); // 按书名排序 manager.sortByName(); // 输出所有图书信息 manager.printAll(); cout << endl; // 按编号排序 manager.sortByID(); // 输出所有图书信息 manager.printAll(); cout << endl; // 查找图书信息 manager.searchBook(2); // 将图书信息保存到文件 manager.saveToFile("book.txt"); // 从文件读取图书信息 manager.readFromFile("book.txt"); // 输出所有图书信息 manager.printAll(); // 释放内存 for (auto book : manager.books) { delete book; } return 0; } ``` 以上只是一个简单的示例,具体的实现可能因需求而有所差异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值