2017-03-31 93 views
0

訪問引用類實例的私有成員的適當方式是什麼?例如,從Cookie的實例訪問initialNumberOfColumns?然後將該值用於其他類成員函數中,例如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(),但我不知道如何,他們可以在成員函數Game()訪問的Cookie類這樣的私有成員返回如下:

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

如果它不是一個私有成員,但公衆一個,你知道該怎麼做?好消息是,返回私人類成員的引用完全相同。 –

+0

我相信如此。我會把'返回cookie'對嗎?但是,這只是給了我''Game'類的私人成員'Cookie cookie'。我如何訪問'cookie.initialNumberOfRows'? – pstatix

+0

@JohnnyMopp:除了'Cookie'沒有出現'initialNumberOfRows'的getter。 –

回答

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); 
} 
+0

second.cpp最初有:'int initialNumberOfRows','int numberOfRows','int numberOfColumns'&'intCookie [MAXROWS]'。我應該刪除它們,因爲它們在first.h和second.h中定義;但是當被移除時,它們因此是不確定的,我需要重新定義它們。通過'return cookie',我可以訪問成員,但我需要以某種方式定義。 – pstatix

+0

我更新了我的答案 –