通过引用访问者访问私有成员(Accessing private members via reference accessor)

编程入门 行业动态 更新时间:2024-10-25 12:23:32
通过引用访问者访问私有成员(Accessing private members via reference accessor)

访问引用的类实例的私有成员的适当方法是什么? 例如,从Cookie实例访问initialNumberOfColumns ? 然后将该值用于另一个类成员函数,例如在引用访问器检索到Cookie类的成员之后的Game::Game() 。

first.h:

class Cookie { public: Cookie(); ~Cookie(); void takeABite (int column, int row); int getNumberOfRows(); int getNumberOfRows(int colNum); int getNumberOfColumns(); int getNumberOfColumns(int rowNum); void display (std::ostream& output); private: int initialNumberOfRows; int numberOfRows; int numberOfColumns int* cookie; };

second.h:

class Game { public: Game(); bool gameEnded(); bool biteIsLegal (int column, int row); Cookie& getCookie(); private: Cookie cookie; };

second.cpp是我遇到困难的地方。 我知道我需要使用Cookie& Game::getCookie()但我不知道如何返回Cookie类的私有成员,以便可以在下面的成员函数Game()中访问它们:

Cookie& Game::getCookie() { return //not sure how to access; } Game::Game() { initialNumberOfRows = numberOfRows = 4 + rand() % (MAXROWS - 4); numberOfColumns = numberOfRows; while (numberOfColumns == numberOfRows) { numberOfColumns = 4 + rand() % (MAXROWS - 4); } cout << "The cookie has " << numberOfRows << " rows of " << numberOfColumns << " columns" << endl; for (int row = 0; row < numberOfRows; ++row) { cookie[row] = numberOfColumns; } }

What is the appropriate way to access private members of a referenced class instance? For example, accessing the initialNumberOfColumns from an instance of Cookie? Then have that value be used in another classes member function, such at Game::Game() after the reference accessor has retrieved the member of the Cookie class.

first.h:

class Cookie { public: Cookie(); ~Cookie(); void takeABite (int column, int row); int getNumberOfRows(); int getNumberOfRows(int colNum); int getNumberOfColumns(); int getNumberOfColumns(int rowNum); void display (std::ostream& output); private: int initialNumberOfRows; int numberOfRows; int numberOfColumns int* cookie; };

second.h:

class Game { public: Game(); bool gameEnded(); bool biteIsLegal (int column, int row); Cookie& getCookie(); private: Cookie cookie; };

second.cpp is where I am having difficult. I know I need to use Cookie& Game::getCookie() but I am not sure how to return the private members of the Cookie class such that they can be accessed in the member function Game() below.:

Cookie& Game::getCookie() { return //not sure how to access; } Game::Game() { initialNumberOfRows = numberOfRows = 4 + rand() % (MAXROWS - 4); numberOfColumns = numberOfRows; while (numberOfColumns == numberOfRows) { numberOfColumns = 4 + rand() % (MAXROWS - 4); } cout << "The cookie has " << numberOfRows << " rows of " << numberOfColumns << " columns" << endl; for (int row = 0; row < numberOfRows; ++row) { cookie[row] = numberOfColumns; } }

最满意答案

Game类有一个cookie成员。 这就是Game::getCookie()方法应该返回的内容:

Cookie& Game::getCookie() { return cookie; }

现在,在Game构造函数内部,您可以直接访问cookie成员,因此您无需使用getCookie()来访问它。 Cookie类具有读取您尝试使用的大多数值的公共方法,但它不提供设置这些成员值的任何访问权限,或者根本不提供对其私有 initialNumberOfRows成员的任何访问权限。

您在Game构造函数中尝试执行的操作应该在Cookie构造函数中完成:

Cookie::Cookie() { initialNumberOfRows = numberOfRows = 4 + rand() % (MAXROWS - 4); numberOfColumns = numberOfRows; while (numberOfColumns == numberOfRows) { numberOfColumns = 4 + rand() % (MAXROWS - 4); } cookie = new int[numberofRows]; for (int row = 0; row < numberOfRows; ++row) { cookie[row] = numberOfColumns; } }

然后Game构造函数可以根据需要记录值:

Game::Game() { cout << "The cookie has " << cookie.getNumberOfRows() << " rows of " << cookie.getNumberOfColumns() << " columns" << endl; }

现在,就是说, Cookie类违反了三法则 。 它需要实现一个复制构造函数复制赋值运算符,以确保其int *cookie字段的完整性:

class Cookie { public: Cookie(); Cookie(const Cookie &src); ~Cookie(); Cookie& operator=(const Cookie &rhs); void swap(Cookie &other); ... private: int initialNumberOfRows; int numberOfRows; int numberOfColumns int* cookie; };

#include <algorithm> Cookie::Cookie() { initialNumberOfRows = numberOfRows = 4 + rand() % (MAXROWS - 4); numberOfColumns = numberOfRows; while (numberOfColumns == numberOfRows) { numberOfColumns = 4 + rand() % (MAXROWS - 4); } cookie = new int[numberOfRows]; std::fill(cookie, cookie + numberOfRows, numberOfColumns); } Cookie::Cookie(const Cookie &src) : initialNumberOfRows(src.initialNumberOfRows), numberOfRows(src.numberOfRows), numberOfColumns(src.numberOfColumns), cookie(new int[numberOfRows]) { std::copy(src.cookie, src.cookie + numberOfRows, cookie); } Cookie::~Cookie() { delete[] cookie; } Cookie& Cookie::operator=(const Cookie &rhs) { if (this != &rhs) Cookie(rhs).swap(*this); return *this; } void Cookie::swap(Cookie &other) { std::swap(initialNumberOfRows, other.initialNumberOfRows); std::swap(numberOfRows, other.numberOfRows); std::swap(numberOfColumns, other.numberOfColumns); std::swap(cookie, other.cookie); }

否则,将int *cookie成员改为std::vector ,让编译器和STL为您处理内存管理的艰苦工作:

#include <vector> class Cookie { public: Cookie(); ... private: int initialNumberOfRows; int numberOfRows; int numberOfColumns std::vector<int> cookie; };

Cookie::Cookie() { initialNumberOfRows = numberOfRows = 4 + rand() % (MAXROWS - 4); numberOfColumns = numberOfRows; while (numberOfColumns == numberOfRows) { numberOfColumns = 4 + rand() % (MAXROWS - 4); } cookie.resize(numberOfRows); std::fill(cookie.begin(), cookie.end(), numberOfColumns); }

The Game class has a cookie member. That is what the Game::getCookie() method should return:

Cookie& Game::getCookie() { return cookie; }

Now, inside of the Game constructor, you have direct access to the cookie member, so you don't need to use getCookie() to access it. And the Cookie class has public methods for reading most of the values you are trying to use, but it DOES NOT provide any access to set those member values, or any access to its private initialNumberOfRows member at all.

The things you are trying to do in the Game constructor should be done in the Cookie constructor instead:

Cookie::Cookie() { initialNumberOfRows = numberOfRows = 4 + rand() % (MAXROWS - 4); numberOfColumns = numberOfRows; while (numberOfColumns == numberOfRows) { numberOfColumns = 4 + rand() % (MAXROWS - 4); } cookie = new int[numberofRows]; for (int row = 0; row < numberOfRows; ++row) { cookie[row] = numberOfColumns; } }

Then the Game constructor can log the values as needed:

Game::Game() { cout << "The cookie has " << cookie.getNumberOfRows() << " rows of " << cookie.getNumberOfColumns() << " columns" << endl; }

Now, that being said, the Cookie class is violating the Rule of Three. It needs to implement a copy constructor and copy assignment operator to ensure the integrity of its int *cookie field:

class Cookie { public: Cookie(); Cookie(const Cookie &src); ~Cookie(); Cookie& operator=(const Cookie &rhs); void swap(Cookie &other); ... private: int initialNumberOfRows; int numberOfRows; int numberOfColumns int* cookie; };

#include <algorithm> Cookie::Cookie() { initialNumberOfRows = numberOfRows = 4 + rand() % (MAXROWS - 4); numberOfColumns = numberOfRows; while (numberOfColumns == numberOfRows) { numberOfColumns = 4 + rand() % (MAXROWS - 4); } cookie = new int[numberOfRows]; std::fill(cookie, cookie + numberOfRows, numberOfColumns); } Cookie::Cookie(const Cookie &src) : initialNumberOfRows(src.initialNumberOfRows), numberOfRows(src.numberOfRows), numberOfColumns(src.numberOfColumns), cookie(new int[numberOfRows]) { std::copy(src.cookie, src.cookie + numberOfRows, cookie); } Cookie::~Cookie() { delete[] cookie; } Cookie& Cookie::operator=(const Cookie &rhs) { if (this != &rhs) Cookie(rhs).swap(*this); return *this; } void Cookie::swap(Cookie &other) { std::swap(initialNumberOfRows, other.initialNumberOfRows); std::swap(numberOfRows, other.numberOfRows); std::swap(numberOfColumns, other.numberOfColumns); std::swap(cookie, other.cookie); }

Otherwise, change the int *cookie member to a std::vector instead, and let the compiler and STL handle the hard work of memory management for you:

#include <vector> class Cookie { public: Cookie(); ... private: int initialNumberOfRows; int numberOfRows; int numberOfColumns std::vector<int> cookie; };

Cookie::Cookie() { initialNumberOfRows = numberOfRows = 4 + rand() % (MAXROWS - 4); numberOfColumns = numberOfRows; while (numberOfColumns == numberOfRows) { numberOfColumns = 4 + rand() % (MAXROWS - 4); } cookie.resize(numberOfRows); std::fill(cookie.begin(), cookie.end(), numberOfColumns); }

更多推荐

本文发布于:2023-08-05 00:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:访问者   成员   Accessing   private   reference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!