CS253

CS253: Software Development with C++

Spring 2018

Const

See this page as a slide show

Const

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”

Modified: 2018-01-07T22:40

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building