C++ 通过引用访问器访问私有成员

C++ 通过引用访问器访问私有成员,c++,C++,访问被引用类实例的私有成员的适当方式是什么?例如,从Cookie的实例访问initialNumberOfColumns?然后,在引用访问器检索到Cookie类的成员后,将该值用于另一个类成员函数中,例如在Game::Game()处 第一,h: class Cookie { public: Cookie(); ~Cookie(); void takeABite (int column, int row); int getNum

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

第一,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;
};
第二,h:

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

    private:
        Cookie cookie;
};
第二,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;
    }
}
Cookie&Game::getCookie(){
return//不确定如何访问;
}
Game::Game(){
initialNumberOfRows=numberOfRows=4+rand()%(MAXROWS-4);
numberOfColumns=numberOfRows;
while(numberOfColumns==numberOfRows){
numberOfColumns=4+rand()%(MAXROWS-4);
}

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

Cookie& Game::getCookie() {
    return cookie;
}
现在,在
Game
构造函数中,您可以直接访问
cookie
成员,因此不需要使用
getCookie()
访问它。而
Cookie
类具有公共方法,用于读取您试图使用的大多数值,但它不提供对设置这些成员值的任何访问,也不提供对其私有
initialNumberOfRows
成员的任何访问

您试图在
游戏
构造函数中执行的操作应该在
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;
    }
}
现在,也就是说,
Cookie
类违反了。它需要实现一个copy构造函数和copy赋值操作符,以确保其
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;
};


如果它不是一个私有成员而是一个公共成员,你知道怎么做吗?好消息,返回对私有类成员的引用的方式完全相同。我相信是这样。我会把
返回cookie
放对吗?但这只会给我类
游戏的
私有成员
cookie
。我如何访问
>cookie.initialNumberOfRows
?@JohnnyMopp:除非
cookie
似乎没有为
initialNumberOfRows
提供getter。您没有询问访问cookie的某些成员。
getCookie()
返回对
Cookie
类实例的引用。您询问了它的返回语句应该是什么。这就是答案。如果这不是您要问的,您需要编辑您的问题,并明确您要问的内容。@RemyLebeau没有注意到这一点。…删除第二个.cpp最初有:
int initialNumberOfRows
int numberOfRows
int numberOfColumns
&
int-cookie[maxrrows]
。我应该删除它们,因为它们在第一个.h和第二个.h中定义;但是删除后,它们就没有定义,我需要重新定义它们。通过
返回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);
}
#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);
}