简体   繁体   English

嵌套类,访问内部私有成员 class

[英]Nested classes, access private members in inner class

My objective is to create a Book class and also a Bookshelf class. This will be a nested class where Bookshelf is the outer- and Book is in the inner class. I've got it to work if all my data members in my Book class is public.我的目标是创建一本书 class 和一个书架 class。这将是一个嵌套的 class,其中书架在外面,而书在里面 class。如果我的书 class 中的所有数据成员,我就可以使用它是公开的。 However if it is private like I want to, I don't get it to work because of my print_bookshelf function. I will get these errors:但是,如果它像我想的那样是私有的,由于我的print_bookshelf function,我无法让它工作。我会收到以下错误:

test.cpp: In member function 'void Bookshelf::print_bookshelf() const':
test.cpp:40:23: error: 'std::string Bookshelf::Book::title' is private within this context
   40 |             cout << e.title << ", " << e.author << ", " << e.pages << endl;
      |                       ^~~~~
test.cpp:16:20: note: declared private here
   16 |             string title {};
      |                    ^~~~~
test.cpp:40:42: error: 'std::string Bookshelf::Book::author' is private within this context
   40 |             cout << e.title << ", " << e.author << ", " << e.pages << endl;
      |                                          ^~~~~~
test.cpp:17:20: note: declared private here
   17 |             string author {};
      |                    ^~~~~~
test.cpp:40:62: error: 'int Bookshelf::Book::pages' is private within this context
   40 |             cout << e.title << ", " << e.author << ", " << e.pages << endl;
      |                                                              ^~~~~
test.cpp:18:17: note: declared private here
   18 |             int pages {};
      |                 ^~~~~

This is my code:这是我的代码:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Bookshelf
{
public:
Bookshelf()
{}

 class Book
    {
    private:
            string title {};
            string author {};
            int pages {};
        
        public:
            Book(string const& t, string const& a, int const p)
                : title{t}, author{a}, pages{p}
            {}

            void print() const
            {
                cout << title << ", " << author << ", " << pages << endl;
            }
        };

    void add_book(Book const& b)
    {
    bookshelf.push_back(b);
    }

    void print_bookshelf () const
    {
        for (auto e : bookshelf)
        {
            cout << e.title << ", " << e.author << ", " << e.pages << endl;
        }
    }

    private:
        vector<Book> bookshelf {};
};

int main()
{
    Bookshelf::Book book_1("Hej", "Me", 100);
    Bookshelf::Book book_2("Yo", "Me", 150);
    Bookshelf bookshelf_1;
    book_1.print();
    bookshelf_1.add_book(book_1);
    bookshelf_1.add_book(book_2);

    bookshelf_1.print_bookshelf();

    return 0;
}

Just change this function只需更改此 function

void print_bookshelf () const
{
    for (auto e : bookshelf)
    {
        cout << e.title << ", " << e.author << ", " << e.pages << endl;
    }
}

the following way以下方式

void print_bookshelf () const
{
    for ( const auto &e : bookshelf )
    {
       e.print();
    }
}

You will avoid code duplication and use the public interface of the class Book.您将避免代码重复并使用 class Book 的公共接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM