#include <stdio.h>

struct Rec {
    const int id;               /* const field: fixed at creation */
    int &counter;               /* reference field: bound at creation */
    int value;                  /* ordinary field */

    Rec(int i, int &c, int v) : id(i), counter(c), value(v) {}
    const Rec &operator=(const Rec &o)
    {
        value = o.value;        /* copy only what can change */
        return *this;
    }
    void Print(const char *name) const
    {
        printf("  %s: id=%d counter=%d value=%d\n",
               name, id, counter, value);
    }
};

int main()
{
    int x = 10, y = 20;
    Rec a(1, x, 100), b(2, y, 200);
    Rec c = a;                  /* copy: c.id = 1, c.counter is x */

    printf("before:\n");
    a.Print("a");
    b.Print("b");
    c.Print("c");

    b = a;                      /* our operator=: only value */
    c.counter++;                /* c.counter is x, so a sees it too */

    printf("after b = a; c.counter++;\n");
    a.Print("a");
    b.Print("b");
    c.Print("c");
    printf("  x = %d, y = %d\n", x, y);
    return 0;
}
