2017-07-11 75 views
-3
class bus { 

private:  
    string arr[10] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; 

public:  
    void reservation(); 

}; 

在這裏,我有一個私人數組,我想訪問並通過公共類中的reservation()進行更改。如何訪問C++私有類中的數組?

void bus::reservaton() { 

cout << "What should I write in here to change the 3rd and 7th index of the 
above array to \"not empty\"" << endl; 

} 

假設我想讓第3和第7個索引「不爲空」,我該寫什麼?

樣品: -

string arr[10] = { "0", "1", "2", "not empty", "4", "5", "6", "not empty", "8", "9" }; 

,做我需要做的主要功能的任何更改?如果是,那麼你可以幫我寫下來。 謝謝。

+0

'arr [3] =「not empty」'? –

+0

成員函數可以訪問私有成員,否則兩者都是相當無用的 – user463035818

+2

在這一點上,最好的行動方式是閱讀一本書或至少一本關於C++的教程。認真。 – rustyx

回答

0

How to use arrays

void bus::reservation() { 
    arr[3] = "not empty"; 
    arr[7] = "not empty"; 
} 
+0

它是合成糖,完整的語法是「this.arr [3] =」not empty「;」 –

+4

@RomanGrout用「wrong」替換「full」並且您的評論是正確的:P'this'是一個指針, this.arr'不會工作。無論如何在會員面前寫'this->'純粹是一種口味問題 – user463035818

+0

@ tobi303不要那麼苛刻......還有一點需要注意的是,有些情況下,this->不僅僅是一個問題的味道:) –