const

e.g.1

#include <iostream>

const int max = 5;

int main(void)
{
    int i;
    int number[max];

    std::cout << "数字を5つ入力してください" << std::endl;
    for(i=0;i<max;i++)
        std::cin >> number[i];

    std::cout << "数字を5つ出力します" << std::endl;
    for(i=0;i<max;i++)
        std::cout << number[i] << " ";

    max=7; // これはerror
    return 0;
}

e.g.2

#include <iostream>

class Name
{
    char name[128];
public:
    Name(const char *s){ SetName(s); }
    void SetName(const char *s){ strcpy(name, s); }
    void PrintName() const { std::cout << name << std::endl; }
};

int main(void)
{
    const Name star("Mickey Rourke");
    star.PrintName();

    return 0;
}

戻る