访问引用的类实例的私有成员的适当方法是什么?例如,从initialNumberOfColumns
的实例访问Cookie
?然后在另一个类成员函数中使用该值,例如在引用访问者检索Game::Game()
类的成员之后Cookie
。
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;
}
}
答案 0 :(得分:1)
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
类违反了Rule of Three。它需要实现复制构造函数和复制赋值运算符以确保其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);
}