#include <stdio.h>

struct s1 {
    int a, b;
};

class Pointer_s1 {
    s1 *p;
public:
    Pointer_s1(s1 *ptr = 0) : p(ptr) {}
    ~Pointer_s1() { if (p) delete p; }
    s1 *operator=(s1 *ptr) {
        if (p)
            delete p;
        p = ptr;
        return p;
    }
    s1 &operator*() const { return *p; }
    s1 *operator->() const { return p; }
private:
    Pointer_s1(const Pointer_s1 &);             /* no copying */
    void operator=(const Pointer_s1 &);         /* no assignments */
};

static void print(const char *what, const s1 &s)    /* takes an s1 */
{
    printf("%-22s a = %d, b = %d\n", what, s.a, s.b);
}

static void twice(s1 &s)                            /* changes an s1 */
{
    s.a *= 2;
    s.b *= 2;
}

int main()
{
    Pointer_s1 p;
    s1 other = { 7, 8 };

    p = new s1;
    (*p).a = 25;                        /* same as p->a = 25 */
    (*p).b = (*p).a + 36;
    print("after (*p).a = ...:", *p);

    *p = other;                         /* copy a whole struct into it */
    print("after *p = other:", *p);

    twice(*p);                          /* pass the object itself */
    print("after twice(*p):", *p);

    s1 copy = *p;                       /* take a copy out */
    copy.a = 0;
    print("copy:", copy);
    print("*p unchanged:", *p);
    return 0;
}
