CS253: Software Development with C++

Spring 2018

Const

See this page as a slide show

CS253 Const

const

Example

const int BOARD_SIZE = 8;
char chessboard[BOARD_SIZE][BOARD_SIZE];
for (int i=0; i<BOARD_SIZE; i++)
    for (int j=0; j<BOARD_SIZE; j++)
        chessboard[i][j] = ' ';

if (chessboard[3][2] == ' ')    // Empty position?
    chessboard[3][2] = 'R';     // put a rook there

cout << chessboard[3][2] << '\n';
R

Reference example

void show_name(const string &name) {
    cout << "The name is: “" << name << "”\n";
}

int main(int, char *argv[]) {
    string s = argv[0];
    show_name(s);
}
The name is: “./a.out”

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:51

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building