const

const なポインタはどっちがどっちだか混乱するなあ.

* の左側に const があるとポインタが指し示すものが const になり,*の右側に const があるとポインタが const になるわけですよね.

// const.cpp
#include <iostream>

int main()
{
    int one = 1;
    int two = 2;
    std::cout << one << std::endl;
    std::cout << two << std::endl;

    int *not_const;
    not_const = &one;
    std::cout << *not_const << std::endl;
    *not_const = 3;
    std::cout << *not_const << std::endl;

    const int *data_is_const = &one;
    data_is_const = &two;
    std::cout << *data_is_const << std::endl;
    //*data_is_const = 3;       これはできない

    int * const pointer_is_const = &one;
    //pointer_is_const = &two;  これはできない
    *pointer_is_const = two;
    std::cout << *pointer_is_const << std::endl;
    std::cout << one << std::endl;  // one の内容も当然変わる

    const int * const both_is_const = &one;
    // どっちもできない
    // both_is_const = &two;
    // *both_is_const = two;

    return 0;
}